use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class AnnotationsUtils method getContent.
public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.Content[] annotationContents, String[] classTypes, String[] methodTypes, Schema schema, Components components, JsonView jsonViewAnnotation) {
if (annotationContents == null || annotationContents.length == 0) {
return Optional.empty();
}
// Encapsulating Content model
Content content = new Content();
for (io.swagger.v3.oas.annotations.media.Content annotationContent : annotationContents) {
MediaType mediaType = new MediaType();
if (components != null) {
getSchema(annotationContent, components, jsonViewAnnotation).ifPresent(mediaType::setSchema);
if (annotationContent.schemaProperties().length > 0) {
if (mediaType.getSchema() == null) {
mediaType.schema(new Schema<Object>().type("object"));
}
Schema oSchema = mediaType.getSchema();
for (SchemaProperty sp : annotationContent.schemaProperties()) {
Class<?> schemaImplementation = sp.schema().implementation();
boolean isArray = false;
if (schemaImplementation == Void.class) {
schemaImplementation = sp.array().schema().implementation();
if (schemaImplementation != Void.class) {
isArray = true;
}
}
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation).ifPresent(s -> {
if ("array".equals(oSchema.getType())) {
oSchema.getItems().addProperty(sp.name(), s);
} else {
oSchema.addProperty(sp.name(), s);
}
});
}
}
if (hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) && mediaType.getSchema() != null && !Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) && !Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation).ifPresent(s -> {
if ("array".equals(mediaType.getSchema().getType())) {
mediaType.getSchema().getItems().additionalProperties(s);
} else {
mediaType.getSchema().additionalProperties(s);
}
});
}
} else {
mediaType.setSchema(schema);
}
ExampleObject[] examples = annotationContent.examples();
if (examples.length == 1 && StringUtils.isBlank(examples[0].name())) {
getExample(examples[0], true).ifPresent(exampleObject -> mediaType.example(exampleObject.getValue()));
} else {
for (ExampleObject example : examples) {
getExample(example).ifPresent(exampleObject -> mediaType.addExamples(example.name(), exampleObject));
}
}
if (annotationContent.extensions() != null && annotationContent.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(annotationContent.extensions());
if (extensions != null) {
extensions.forEach(mediaType::addExtension);
}
}
io.swagger.v3.oas.annotations.media.Encoding[] encodings = annotationContent.encoding();
for (io.swagger.v3.oas.annotations.media.Encoding encoding : encodings) {
addEncodingToMediaType(mediaType, encoding, jsonViewAnnotation);
}
if (StringUtils.isNotBlank(annotationContent.mediaType())) {
content.addMediaType(annotationContent.mediaType(), mediaType);
} else {
applyTypes(classTypes, methodTypes, content, mediaType);
}
}
if (content.size() == 0) {
return Optional.empty();
}
return Optional.of(content);
}
use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.
the class ReaderTest method testExampleWithRef.
@Test(description = "Example with ref")
public void testExampleWithRef() {
Components components = new Components();
components.addExamples("Id", new Example().description("Id Example").summary("Id Example").value("1"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(RefExamplesResource.class);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /example:\n" + " post:\n" + " description: subscribes a client to updates relevant to the requestor's account\n" + " operationId: subscribe\n" + " parameters:\n" + " - name: subscriptionId\n" + " in: path\n" + " required: true\n" + " style: simple\n" + " schema:\n" + " type: string\n" + " description: Schema\n" + " example: Subscription example\n" + " examples:\n" + " subscriptionId_1:\n" + " summary: Subscription number 12345\n" + " description: subscriptionId_1\n" + " value: 12345\n" + " externalValue: Subscription external value 1\n" + " $ref: '#/components/examples/Id'\n" + " example: example\n" + " requestBody:\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: integer\n" + " format: int32\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/SubscriptionResponse'\n" + "components:\n" + " schemas:\n" + " SubscriptionResponse:\n" + " type: object\n" + " properties:\n" + " subscriptionId:\n" + " type: string\n" + " examples:\n" + " Id:\n" + " summary: Id Example\n" + " description: Id Example\n" + " value: \"1\"\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Aggregations