@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);
@Test(description = "it should serialize an array BodyParameter") public void serializeArrayBodyParameter() { final Schema model = new ArraySchema().items(new Schema().$ref("#/definitions/Cat")); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Cat\"}}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "it should serialize a BodyParameter") public void serializeBodyParameter() { final Schema model = new Schema() .title("Cat") .addProperties("name", new StringSchema()); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"title\":\"Cat\",\"properties\":{\"name\":{\"type\":\"string\"}}}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
@Test(description = "it should serialize a BodyParameter to yaml") public void serializeBodyParameterToYaml() { final Schema model = new Schema() .title("Cat") .addProperties("name", new StringSchema()); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String yaml = "---\n" + "content:\n" + " '*/*':\n" + " schema:\n" + " title: Cat\n" + " properties:\n" + " name:\n" + " type: string"; SerializationMatchers.assertEqualsToYaml(p, yaml); }
/** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#media-type-object * * @param protoType * @return */ protected MediaType getMediaType(ProtoType protoType) { MediaType mediaType = new MediaType(); mediaType.schema(getSchema(protoType)); // mediaType.encoding(); // mediaType. return mediaType; }
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 RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) { RequestBody body = new RequestBody(); BodyParameter bp = (BodyParameter) param; List<String> mediaTypes = new ArrayList<>(globalConsumes); if (consumes != null && consumes.size() > 0) { mediaTypes.clear(); mediaTypes.addAll(consumes); } if (mediaTypes.size() == 0) { mediaTypes.add("*/*"); } if (StringUtils.isNotBlank(param.getDescription())) { body.description(param.getDescription()); } body.required(param.getRequired()); Content content = new Content(); for (String type : mediaTypes) { content.addMediaType(type, new MediaType().schema( convert(bp.getSchema()))); if (StringUtils.isNotBlank(bp.getDescription())) { body.setDescription(bp.getDescription()); } } convertExamples(((BodyParameter) param).getExamples(), content); body.content(content); return body; }
private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) { RequestBody body = new RequestBody(); BodyParameter bp = (BodyParameter) param; List<String> mediaTypes = new ArrayList<>(globalConsumes); if (consumes != null && consumes.size() > 0) { mediaTypes.clear(); mediaTypes.addAll(consumes); } if (mediaTypes.size() == 0) { mediaTypes.add("*/*"); } if (StringUtils.isNotBlank(param.getDescription())) { body.description(param.getDescription()); } body.required(param.getRequired()); Content content = new Content(); for (String type : mediaTypes) { content.addMediaType(type, new MediaType().schema( convert(bp.getSchema()))); if (StringUtils.isNotBlank(bp.getDescription())) { body.setDescription(bp.getDescription()); } } convertExamples(((BodyParameter) param).getExamples(), content); body.content(content); return body; }
private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) { RequestBody body = new RequestBody(); BodyParameter bp = (BodyParameter) param; List<String> mediaTypes = new ArrayList<>(globalConsumes); if (consumes != null && consumes.size() > 0) { mediaTypes.clear(); mediaTypes.addAll(consumes); } if (mediaTypes.size() == 0) { mediaTypes.add("*/*"); } if (StringUtils.isNotBlank(param.getDescription())) { body.description(param.getDescription()); } body.required(param.getRequired()); Content content = new Content(); for (String type : mediaTypes) { content.addMediaType(type, new MediaType().schema( convert(bp.getSchema()))); if (StringUtils.isNotBlank(bp.getDescription())) { body.setDescription(bp.getDescription()); } } convertExamples(((BodyParameter) param).getExamples(), content); body.content(content); return body; }
content.addMediaType(type, new MediaType().schema(formSchema));
content.addMediaType(type, new MediaType().schema(formSchema));
private Content readAsContent(ParseContext parseCtx, Type responseClass) { if (responseClass == null) { return null; } OpenAPI openAPI = parseCtx.openAPI(); Content content = new Content(); MediaType mediaType = new MediaType(); ResolvedSchema resolvedSchema = modelConverters().resolveAsResolvedSchema(new AnnotatedType(responseClass).resolveAsRef(true)); mediaType.schema(resolvedSchema.schema); content.addMediaType(MediaTypeConstants.DEFAULT_JSON_BODY_TYPE, mediaType); resolvedSchema.referencedSchemas.forEach((name, schema) -> { openAPI.schema(name, schema); }); return content; }
content.addMediaType(type, new MediaType().schema(formSchema));
content.addMediaType(type, mediaType.schema(schema));
content.addMediaType(type, mediaType.schema(schema));
content.addMediaType(type, mediaType.schema(schema));
javax.ws.rs.core.MediaType.APPLICATION_JSON, new MediaType().schema(new Schema<ErrorTO>())); content.addMediaType( RESTHeaders.APPLICATION_YAML, new MediaType().schema(new Schema<ErrorTO>())); content.addMediaType( javax.ws.rs.core.MediaType.APPLICATION_XML, new MediaType().schema(new Schema<ErrorTO>()));