Validation of OpenAPI schema fails with unreasonable error of missing items of a schema
Hello!
I am implementing tests to verify the response from the API against the written schema in OpenAPI. I am using RestAssured with this setup:
OpenApiValidationFilter validationFilter = new OpenApiValidationFilter("src/main/resources/META-INF/openapi.yml");
RestAssured.filters(validationFilter);
The validation fails with:
com.atlassian.oai.validator.OpenApiInteractionValidator$ApiLoadException: Unable to load API spec from provided URL or payload:
- attribute components.schemas.Error.items is missing
at com.atlassian.oai.validator.util.OpenApiLoader.loadApi(OpenApiLoader.java:42)
at com.atlassian.oai.validator.OpenApiInteractionValidator$Builder.build(OpenApiInteractionValidator.java:541)
at com.atlassian.oai.validator.restassured.OpenApiValidationFilter.<init>(OpenApiValidationFilter.java:41)
This is the mentioned OpenAPI.yml
ErrorResponseBody:
type: object
properties:
errors:
type: array
items:
$ref: "#/components/schemas/Error"
Error:
type: object
additionalProperties: true
required:
- code
- message
- type
properties:
code:
type: number
format: int32
message:
type: string
attributePath:
type: string
givenValue:
anyOf:
- type: string
- type: number
- type: boolean
- type: array
- type: object
type:
type: string
enum:
- UNKNOWN
- RANGE_VIOLATION
- ENTITY_NOT_FOUND
- VALUE_REQUIRED
- UNREASONABLE_CHANGE
- INVALID_JSON
- LENGTH_VIOLATION
- NON_EXISTING_ENUM_VALUE
- WRONG_FORMAT_ATTRIBUTE
- UNKNOWN_PROPERTY
- DUPLICATE_ENTITY
- NOT_EXISTING_ENUM_VALUE
- INVALID_VALUe
These are the lines in which the error is added to the result:
if("array".equals( schema.getType()) && !(schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null)) {
result.missing(location, "items");
}
I debugged to the io.swagger.v3.parser.util.OpenAPIDeserializer
and found out
- the created schema is a plain
Schema
, noArraySchema
therefore, no items exists - the schema type is indeed
array
Am I missing something here? What exactly is wrong? Other test frameworks like https://www.chaijs.com/plugins/chai-openapi-response-validator/ are working fine with the same test-setup.
Regards,
Alexander
Comments (4)
-
reporter -
If an array is defined, those items shall be defined, too.
Have you tried it like this?
givenValue: anyOf: - type: string - type: number - type: boolean - type: array items: type: object - type: object
-
As Sven has said - the example you’ve given is not valid according to the OpenAPI v3 spec. According to the specification the
items
keyword is required fortype: array
(https://swagger.io/specification/#schema-object)items - Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
items
MUST be present if thetype
isarray
.The error you’re seeing is coming from the underlying OpenAPI parsing library.
I’ll take a look at how I can surface these errors with more information, but in the meantime the correct fix is as shown by Sven.
-
- changed status to resolved
- Log in to comment
Something regarding the type: array does not work. Not clue.
I removed it and proceed with: