After seeing the Space Camp Video API Contract Testing | Postman Space Camp - YouTube I was very interested to try the Postman collection from @allenheltondev
So I started to write an OpenApi Spec for a project of ours. I tried to keep it simple and started with our /health endpoint which just returns a string. But I can’t get it to work.
When I change the endpoint and Spec to return an object in application/json it works just fine.
So the question is: Should this collection work with a text/plain response?
Details:
This is a spec that is working:
paths:
/health:
get:
summary: "GET health"
responses:
"200":
description: "OK"
content:
application/json:
schema:
$ref: '#/components/schemas/Uuid'
components:
schemas:
Uuid:
type: object
properties:
uuid:
type: string
description: a randomly generated uuid
example: 'b343a68d-d10b-427c-b4d5-7e8f231c4920'
But when I put it like this for the simple String response:
paths:
/health:
get:
summary: "GET health"
responses:
"200":
description: "OK"
content:
text/plain:
schema:
$ref: '#/components/schemas/Uuid'
components:
schemas:
Uuid:
type: string
description: a randomly generated uuid
example: 'b343a68d-d10b-427c-b4d5-7e8f231c4920'
The ‘Test Request’ request then fails with
GET - /health - No Request Body - SUCCESS: true - Has expected response body schema | TypeError: Cannot convert undefined or null to object
A little console.log magic shows that expectedResponse is only
{statusCode: 200}
which leads the code to the line
return (Object.keys(responseSchema).length == 0);
where responseSchema is undefined
On an application/json response the expectedResponse variable looks like
{statusCode: 200, $ref: "#/components/schemas/Uuid"}
with this ‘$ref’ property the code takes completely other turns.
So to summarise the questions:
- Should the collection work with text/plain
- If yes what do I need to change in my openApi spec