We are building a bunch of APIs and coming into a strange issue when testing them.
In our swagger file we have the following:
"CustomerAddress": {
"type": "object",
"properties": {
"id": {
"$ref": "#/components/schemas/StringSettable"
},
And this references the following:
"StringSettable": {
"type": "object",
"properties": {
"hasValue": {
"type": "boolean"
},
"value": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
This all looks OK to us but when we run a ReadyAPI test we get the following error:
instance type (string) does not match any allowed primitive type (allowed ["object"]) checked node: [/id] corresponding schema: [/properties/id]
This is the response payload in JSON:
{
"id" : "thisisanid",
"flat" : "Flat",
"buildingName" : "Building",
"houseNumber" : 11,
"addressLine1" : "Line 1",
"addressLine2" : "Line 2",
"town" : "Town",
"county" : "County",
"locality" : "Locality",
"district" : "District",
"postcode" : "TE57 ING",
"validFrom" : null,
"country" : "United Kingdom",
"isForeignAddress" : false,
"isUnknownAddress" : false,
"addressType" : 1,
"op" : "add"
},
We have tried editting the Swagger to be the following:
"id": {
"type": "string",
"nullable": true
}
This then passes with no issues so not 100% sure where the assertion is being tripped up on this. Has anyone else encountered this?
The reason this is not working is:
id in the OpenAPI specification is defined as an object:
"StringSettable": {
"type": "object", // <-- it's an object
...
}
but, id in the request payload is defined as a string:
{
"id" : "thisisanid", // <-- it's a string
...
}
In this context, the error message you are getting makes quite a lot of sense:
instance type (string) does not match any allowed primitive type (allowed ["object"])
If you want to fix this problem you have two choices. The first choice you have already discovered - redefine the id type in the OpenAPI specification.
The second choice is to change the request payload so that the schema definition is satisfied:
{
"id" : { // <-- now it's an object!
"hasValue": true,
"value": "thisisanid"
},
...
}