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 serialize a ref BodyParameter") public void serializeRefBodyParameter() { final Schema model = new Schema().$ref("#/definitions/Cat"); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/definitions/Cat\"}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
Schema returnTypeSchema = resolvedSchema.schema; Content content = new Content(); MediaType mediaType = new MediaType().schema(returnTypeSchema); AnnotationsUtils.applyTypes(classProduces == null ? new String[0] : classProduces.value(), methodProduces == null ? new String[0] : methodProduces.value(), content, mediaType); } 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); MediaType mediaType = new MediaType(); AnnotationsUtils.applyTypes(classProduces == null ? new String[0] : classProduces.value(), methodProduces == null ? new String[0] : methodProduces.value(), content, mediaType);
MediaType mediaType = new MediaType(); if (components != null) { getSchema(annotationContent, components, jsonViewAnnotation).ifPresent(mediaType::setSchema); } else { mediaType.setSchema(schema); getExample(example).ifPresent(exampleObject -> mediaType.addExamples(example.name(), exampleObject)); if (extensions != null) { for (String ext : extensions.keySet()) { mediaType.addExtension(ext, extensions.get(ext));
@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)); } }
if (requestBodyParameter.getSchema() != null) { for (MediaType mediaType : requestBody.getContent().values()) { if (mediaType.getSchema() == null) { if (requestBodyParameter.getSchema() == null) { mediaType.setSchema(new Schema()); } else { mediaType.setSchema(requestBodyParameter.getSchema()); if (StringUtils.isBlank(mediaType.getSchema().getType())) { mediaType.getSchema().setType(requestBodyParameter.getSchema().getType());
Map<String,MediaType> content = parameter.getContent(); for (String key: content.keySet()){ if (content.get(key) != null && content.get(key).getSchema() != null ){ Schema resolvedSchema = resolveSchema(content.get(key).getSchema()); if (resolvedSchema != null) { content.get(key).setSchema(resolvedSchema); Map<String, MediaType> content = requestBody.getContent(); for (String key : content.keySet()) { if (content.get(key) != null && content.get(key).getSchema() != null) { Schema resolved = resolveSchema(content.get(key).getSchema()); if (resolved != null) { content.get(key).setSchema(resolved); Map<String, MediaType> content = response.getContent(); for(String mediaType: content.keySet()){ if(content.get(mediaType).getSchema() != null) { Schema resolved = resolveSchema(content.get(mediaType).getSchema()); response.getContent().get(mediaType).setSchema(resolved); if(content.get(mediaType).getExamples() != null) { Map<String,Example> resolved = resolveExample(content.get(mediaType).getExamples()); response.getContent().get(mediaType).setExamples(resolved);
return null; MediaType mediaType = new MediaType(); mediaType.setSchema(getSchema(schemaObject,String.format("%s.%s", location, "schema"),result)); mediaType.setEncoding(getEncodingMap(encodingObject, String.format("%s.%s", location, "encoding"), result)); mediaType.setExtensions(extensions); mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result)); mediaType.setExample(example);
private void declareGetResponseFormats(OpenAPI api, String path, Class<?> binding) { PathItem pi = api.getPaths().get(path); Operation get = pi.getGet(); Content content = get.getResponses().get("200").getContent(); List<String> formats = DefaultWebFeatureService30.getAvailableFormats(binding); // first remove the ones missing Set<String> missingFormats = new HashSet<>(content.keySet()); missingFormats.removeAll(formats); missingFormats.forEach(f -> content.remove(f)); // then add the ones not already declared Set<String> extraFormats = new HashSet<>(formats); extraFormats.removeAll(content.keySet()); for (String extraFormat : extraFormats) { MediaType mediaType = new MediaType(); if (extraFormat.contains("yaml") && content.get("application/json") != null) { // same schema as JSON mediaType.schema(content.get("application/json").getSchema()); } else if (extraFormat.contains("text")) { mediaType.schema(new StringSchema()); } else { mediaType.schema(new BinarySchema()); } content.addMediaType(extraFormat, 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; }
@Override public BallerinaMediaType buildContext(MediaType mediaType, OpenAPI openAPI) throws BallerinaOpenApiException { this.example = mediaType.getExample(); this.extensions = mediaType.getExtensions(); this.encoding = mediaType.getEncoding(); this.examples = mediaType.getExamples(); this.schema = new BallerinaSchema().buildContext(mediaType.getSchema(), openAPI); return this; }
.description("pets returned") .content(new Content() .addMediaType("application/json", new MediaType() .schema(new Schema().$ref("Person")) .example("fun"))); .addMediaType("application/json", new MediaType() .schema(new Schema().$ref("Error")))); .requestBody(new RequestBody() .description("the pet to add") .content(new Content().addMediaType("*/*", new MediaType() .schema(new Schema().$ref("Person")))));
@Test(description = "it should create a response") public void createResponse() throws IOException { final ApiResponse response = new ApiResponse() .content(new Content() .addMediaType("application/json", new MediaType() .addExamples("test", new Example().value("{\"name\":\"Fred\",\"id\":123456\"}")))); final String json = "{\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"examples\" : {\n" + " \"test\" : {\n" + " \"value\" : \"{\\\"name\\\":\\\"Fred\\\",\\\"id\\\":123456\\\"}\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; SerializationMatchers.assertEqualsToJson(response, json); } }
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); } }
/** * Return the example value of the parameter. * * @param codegenParameter Codegen parameter * @param requestBody Request body */ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) { Content content = requestBody.getContent(); if (content.size() > 1) { // @see ModelUtils.getSchemaFromContent() LOGGER.warn("Multiple MediaTypes found, using only the first one"); } MediaType mediaType = content.values().iterator().next(); if (mediaType.getExample() != null) { codegenParameter.example = mediaType.getExample().toString(); return; } if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) { Example example = mediaType.getExamples().values().iterator().next(); if (example.getValue() != null) { codegenParameter.example = example.getValue().toString(); return; } } setParameterExampleValue(codegenParameter); }
private void injectApiVersionToPaths(Paths paths, BiFunction<String, String, String> replacer) { paths.forEach((endpoint, path) -> { path.readOperations().forEach(operation -> { operation.setDescription(replacer.apply(operation.getDescription(), apiVersion.get())); operation.getParameters().forEach(param -> param.setDescription(replacer.apply(param.getDescription(), apiVersion.get()))); operation.getResponses().forEach((name, response) -> { response.setDescription(replacer.apply(response.getDescription(), apiVersion.get())); if (response.getContent() != null) { response.getContent().forEach((mediaTypeName, mediaType) -> { if (mediaType.getExample() != null) { mediaType.setExample(replacer.apply(mediaType.getExample().toString(), apiVersion.get())); } }); } }); }); }); }
if (requestBodyParameter.getSchema() != null) { for (MediaType mediaType : requestBody.getContent().values()) { if (mediaType.getSchema() == null) { if (requestBodyParameter.getSchema() == null) { mediaType.setSchema(new Schema()); } else { mediaType.setSchema(requestBodyParameter.getSchema()); if (StringUtils.isBlank(mediaType.getSchema().getType())) { mediaType.getSchema().setType(requestBodyParameter.getSchema().getType());
protected void updateLocalRefs(ApiResponse response, String pathRef) { if(response.getContent() != null) { Map<String, MediaType> content = response.getContent(); for (String key: content.keySet()) { MediaType mediaType = content.get(key); if (mediaType.getSchema() != null) { updateLocalRefs(mediaType.getSchema(), pathRef); } Map<String, Example> examples = content.get(key).getExamples(); if (examples != null) { for( Example example:examples.values()){ updateLocalRefs(example, pathRef); } } } } }
Map<String,MediaType> content = parameter.getContent(); for (String key: content.keySet()){ if (content.get(key) != null && content.get(key).getSchema() != null ){ Schema resolvedSchema = resolveSchema(content.get(key).getSchema()); if (resolvedSchema != null) { content.get(key).setSchema(resolvedSchema); Map<String, MediaType> content = requestBody.getContent(); for (String key : content.keySet()) { if (content.get(key) != null && content.get(key).getSchema() != null) { Schema resolved = resolveSchema(content.get(key).getSchema()); if (resolved != null) { content.get(key).setSchema(resolved); Map<String, MediaType> content = response.getContent(); for(String mediaType: content.keySet()){ if(content.get(mediaType).getSchema() != null) { Schema resolved = resolveSchema(content.get(mediaType).getSchema()); response.getContent().get(mediaType).setSchema(resolved); if(content.get(mediaType).getExamples() != null) { Map<String,Example> resolved = resolveExample(content.get(mediaType).getExamples()); response.getContent().get(mediaType).setExamples(resolved);