private void addContentSchemaRef(Content content, Set<String> referencedDefinitions) { if (content != null) { for (String keyBodyContent : content.keySet()) { MediaType mediaType = content.get(keyBodyContent); addSchemaRef(mediaType.getSchema(), referencedDefinitions); } } }
@Test(description = "it should deserialize a read only parameter") public void deserializeReadOnlyParameter() throws IOException { final String json = "{\"in\":\"path\"," + "\"content\":{" + " \"*/*\":{" + " \"schema\":{" + " \"type\":\"string\"," + " \"readOnly\":true}}}}"; final Parameter p = m.readValue(json, Parameter.class); assertTrue(p.getContent().get("*/*").getSchema().getReadOnly()); }
@Test(description = "it should deserialize a boolean additionalProperties") public void testBooleanAdditionalPropertiesDeserialization() throws Exception { Operation operation = Json.mapper().readValue(jsonAdditionalPropertiesBoolean, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); assertTrue(responseSchema instanceof ObjectSchema); assertTrue(responseSchema.getAdditionalProperties() instanceof Boolean); }
@Test(description = "vendor extensions should be included with object type") public void testMapDeserializationVendorExtensions() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getExtensions().size() > 0); assertNotNull(mp.getExtensions().get("x-foo")); assertEquals(mp.getExtensions().get("x-foo"), "vendor x"); }
@Test(description = "it should deserialize a response per #1349") public void testMapDeserialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); assertTrue(responseSchema instanceof MapSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getAdditionalProperties() instanceof IntegerSchema); }
@Test(description = "it should includes the example in the arrayproperty") public void testArrayDeserialization() throws Exception { Operation operation = Yaml.mapper().readValue(yaml, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); MediaType media = response.getContent().get("*/*"); Schema responseSchema = media.getSchema(); assertTrue(media.getExamples().size() == 2); assertNotNull(responseSchema); assertTrue(responseSchema instanceof ArraySchema); ArraySchema mp = (ArraySchema) responseSchema; assertEquals(mp.getMinItems(), new Integer(3)); assertEquals(mp.getMaxItems(), new Integer(100)); } }
@Test(description = "it should read an example within an inlined schema") public void testIssue1261InlineSchemaExample() throws Exception { Operation operation = Yaml.mapper().readValue( " responses:\n" + " 200:\n" + " content:\n" + " '*/*':\n" + " description: OK\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int32\n" + " name:\n" + " type: string\n" + " required: [id, name]\n" + " example: ok", Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema schema = response.getContent().get("*/*").getSchema(); Object example = schema.getExample(); assertNotNull(example); assertTrue(example instanceof String); assertEquals(example, "ok"); } }
@Test(description = "it should serialize a boolean additionalProperties") public void testBooleanAdditionalPropertiesSerialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); Schema schema = new ObjectSchema().additionalProperties(true); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : true\n" + "}"); schema = new ObjectSchema().additionalProperties(responseSchema); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " },\n" + " \"x-foo\" : \"vendor x\"\n" + " }\n" + "}"); }
} else if (parentRequestBody.getContent() != null){ for (String parentMediaType: parentRequestBody.getContent().keySet()) { if (content.get(parentMediaType) == null) { content.addMediaType(parentMediaType, parentRequestBody.getContent().get(parentMediaType)); } else { for (String key : operation.getResponses().getDefault().getContent().keySet()) { if (operation.getResponses().getDefault().getContent().get(key).getSchema() == null) { operation.getResponses().getDefault().getContent().get(key).setSchema(returnTypeSchema);
private boolean containsFormContentType(RequestBody body) { if (body == null) { return false; } final Content content = body.getContent(); if (content == null || content.isEmpty()) { return false; } return content.get("application/x-www-form-urlencoded") != null || content.get("multipart/form-data") != null; }
private boolean containsFormContentType(RequestBody body) { if (body == null) { return false; } final Content content = body.getContent(); if (content == null || content.isEmpty()) { return false; } return content.get("application/x-www-form-urlencoded") != null || content.get("multipart/form-data") != null; }
private Optional<Pair<String, MediaType>> findApiMediaTypeForRequest(final Request request, @Nullable final RequestBody apiRequestBodyDefinition) { if (apiRequestBodyDefinition == null || apiRequestBodyDefinition.getContent() == null) { return Optional.empty(); } final Optional<String> mostSpecificMatch = findMostSpecificMatch(request, apiRequestBodyDefinition.getContent().keySet()); if (!mostSpecificMatch.isPresent()) { return Optional.empty(); } final MediaType mediaType = apiRequestBodyDefinition.getContent().get(mostSpecificMatch.get()); return Optional.of(Pair.of(mostSpecificMatch.get(), mediaType)); }
private Content convertExamples(final Map examples, final Content content) { if (examples != null) { examples.forEach((k, v) -> { MediaType mT = content.get(k); if (mT == null) { mT = new MediaType(); content.addMediaType(k.toString(), mT); } mT.setExample(v); }); } return content; }
private Content convertExamples(final Map examples, final Content content) { if (examples != null) { examples.forEach((k, v) -> { MediaType mT = content.get(k); if (mT == null) { mT = new MediaType(); content.addMediaType(k.toString(), mT); } mT.setExample(v); }); } return content; }
private Content convertExamples(final Map examples, final Content content) { if (examples != null) { examples.forEach((k, v) -> { MediaType mT = content.get(k); if (mT == null) { mT = new MediaType(); content.addMediaType(k.toString(), mT); } mT.setExample(v); }); } return content; }
protected Schema getSchemaFromBody(RequestBody requestBody) { String contentType = new ArrayList<>(requestBody.getContent().keySet()).get(0); MediaType mediaType = requestBody.getContent().get(contentType); return mediaType.getSchema(); }
protected Schema getSchemaFromBody(RequestBody requestBody) { String contentType = new ArrayList<>(requestBody.getContent().keySet()).get(0); MediaType mediaType = requestBody.getContent().get(contentType); return mediaType.getSchema(); }
private void addContentSchemaRef(Content content, Set<String> referencedDefinitions) { if (content != null) { for (String keyBodyContent : content.keySet()) { MediaType mediaType = content.get(keyBodyContent); addSchemaRef(mediaType.getSchema(), referencedDefinitions); } } }
private void setMediaTypeToContent(Content content, String mediaTypeName, String jsonExample, boolean overrideExistingExample) { MediaType mediaType = content.get(mediaTypeName); if (mediaType == null) { mediaType = new MediaType(); content.addMediaType(mediaTypeName, mediaType); } if (mediaType.getExample() == null || overrideExistingExample) { mediaType.example(jsonExample); } }
protected <T> void updateLocalRefs(String file, T result) { if(result instanceof ApiResponse) { ApiResponse response = (ApiResponse) result; if (response.getContent() != null) { for (String mediaType : response.getContent().keySet()) { updateLocalRefs(file, response.getContent().get(mediaType).getSchema()); } } } if(result instanceof Schema && ((Schema)(result)).get$ref() != null) { Schema prop = (Schema) result; updateLocalRefs(file, prop); } else if(result instanceof Schema) { Schema model = (Schema) result; updateLocalRefs(file, model); } }