OpenApiInteractionValidator cannot be reused (stateful?)
Issue #384
new
I try to validate the schema with com.atlassian.oai:swagger-request-validator-core:2.27.1
and com.atlassian.oai:swagger-request-validator-mockmvc:2.27.1
. Here is an excerpt of the test code (written in kotlin):
val result = mockMvc.perform(get(url))
.andExpect(status().is2xxSuccessful)
.andExpect(isSwaggerValid()) // <= place where it breaks
.andReturn()
I have written 3 different versions of the isSwaggerValid()
-Method:
companion object { // These kotlin variables are like static fields in Java => common instances to all the tests
val validatorBuilder = OpenApiInteractionValidator.createForInlineApiSpecification(SwaggerSchema.publicApi)
val openApiInteractionValidator= validatorBuilder.build()
val resultMatcher = OpenApiValidationMatchers.openApi().isValid(openApiInteractionValidator)
}
fun isSwaggerValid_OK(): ResultMatcher {
// if I only reuse the validatorBuilder, everything works as expected
return OpenApiValidationMatchers.openApi().isValid(validatorBuilder.build())
}
fun isSwaggerValid_Failing(): ResultMatcher {
// This fails => openApiInteractionValidator is not stateless and breaks
return OpenApiValidationMatchers.openApi().isValid(openApiInteractionValidator)
}
fun isSwaggerValid_Failing2(): ResultMatcher {
// This also fails, which confirms the issue is with the openApiInteractionValidator
return resultMatcher
}
Only the first version (isSwaggerValid_OK
) works when it is called multiple times. The other implementations fail on some tests.
Exception message:
com.atlassian.oai.validator.mockmvc.OpenApiMatchers$OpenApiValidationException: {
"messages" : [ {
"key" : "validation.response.body.schema.type",
"level" : "ERROR",
"message" : "[Path '/alerts/0'] Instance type (string) does not match any allowed primitive type (allowed: [\"object\"])",
"context" : {
"requestPath" : "/accounts/66466515-c186-4792-b60a-4cd9c299e211/alerts/ids",
"responseStatus" : 200,
"location" : "RESPONSE",
"pointers" : {
"instance" : "/alerts/0",
"schema" : "/properties/alerts/items"
},
"requestMethod" : "GET"
}
} ]
}
at app//com.atlassian.oai.validator.mockmvc.OpenApiMatchers.lambda$isValid$0(OpenApiMatchers.java:58)
at app//org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:212)
This happened when validating this body {"alerts":["d2b2cd6f-8349-4d1f-9629-572e5dc64e5a"]}
, against the following entry of our openapi Json-Definition:
"AlertIdsListResponse": {
"required": [
"alerts"
],
"type": "object",
"properties": {
"alerts": {
"type": "array",
"items": {
"type": "string"
}
},
"next": {
"$ref": "#/components/schemas/PaginationNextChunk"
}
}
}