oneOf response object fails "Object instance has properties which are not allowed by the schema"
I’ve been struggling with this issue for a couple of days, and I just can’t find my way out of this…
Using the very basic PetStore example, I’ve changed the response of GET /pet/:petId to return oneOf [User, Pet].
I then created a pet object, and tested to see if I can get it as a response. The test fails on “Object instance has properties which are not allowed by the schema: ["category","id","name","photoUrls","status","tags"]”.
Test code (A bit messy. I brought everything together to make it possible to run):
@Test
public void testValidPetResponseGetPetOneOf(){
try {
String body = "";
ClassLoader cl = getClass().getClassLoader();
URL resource = cl.getResource("examplePet.json");
File file = new File(resource.getFile());
StringBuilder stringBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
Stream<String> stream = br.lines();
stream.forEach(stringBuilder::append);
body = stringBuilder.toString();
final String method = "get";
final String path = "/pet/213";
Map<String, List<String>> headers = new HashMap<>();
List<String> contentType = new ArrayList<>();
contentType.add("application/json");
headers.put("Content-Type", contentType);
final List<String> messages = validateResponseSchema(path, method, 200, headers, body);
messages.forEach(System.out::println);
Assert.assertEquals(messages.size(), 0);
}
catch (Exception e) {
Assert.fail(e.getMessage());
}
}
List<String> validateResponseSchema(String path, String method, int status, Map<String, List<String>> headers, String body) {
ClassLoader cl = getClass().getClassLoader();
URL resource = cl.getResource("petstore-openapi.json");
OpenApiInteractionValidator.Builder builder = OpenApiInteractionValidator.createFor(resource.getFile());
OpenApiInteractionValidator validator = builder.build();
SimpleResponse.Builder responseBuilder = new SimpleResponse.Builder(status);
headers.forEach((headerName, headerValues) -> headerValues.forEach(headerValue -> responseBuilder.withHeader(headerName, headerValue)));
if (body != null && !"".equals(body)) {
responseBuilder.withBody(body);
}
Response response = responseBuilder.build();
ValidationReport validationReport = validator.validateResponse(path, Request.Method.valueOf(method.toUpperCase()), response);
List<String> messages = new ArrayList<>();
validationReport.getMessages().forEach(message -> messages.add(message.getMessage()));
return messages;
}
Pet Object:
{
"photoUrls": [
"photoUrls",
"photoUrls"
],
"name": "doggie",
"id": 0,
"category": {
"name": "sdsd",
"id": 12
},
"tags": [
{
"name": "name",
"id": 1
},
{
"name": "name",
"id": 1
}
],
"status": "available"
}
Pom dependencies:
<dependency>
<groupId>org.openapitools.swagger.parser</groupId>
<artifactId>swagger-parser</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.atlassian.oai</groupId>
<artifactId>swagger-request-validator-core</artifactId>
<version>2.8.1</version>
</dependency>
Any help will be highly appreciated. Thanks in advance
Comments (2)
-
reporter -
reporter - changed status to resolved
The problem is a non-issue. I missed the FAQ page, where it addressed this exact case. Leaving the issue here and not deleting for the next person who will search the issues and not the FAQ….
https://bitbucket.org/atlassian/swagger-request-validator/src/master/docs/FAQ.md - Log in to comment
OpenAPI schema (excerpt)