Search in sources :

Example 31 with Example

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);
}
Also used : SchemaProperty(io.swagger.v3.oas.annotations.media.SchemaProperty) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) ResolvedSchema(io.swagger.v3.core.converter.ResolvedSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) Encoding(io.swagger.v3.oas.models.media.Encoding) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Example 32 with Example

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);
}
Also used : Components(io.swagger.v3.oas.models.Components) Example(io.swagger.v3.oas.models.examples.Example) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)28 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Schema (io.swagger.v3.oas.models.media.Schema)12 Components (io.swagger.v3.oas.models.Components)9 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)9 Info (io.swagger.v3.oas.models.info.Info)8 StringSchema (io.swagger.v3.oas.models.media.StringSchema)7 Example (io.swagger.v3.oas.models.examples.Example)6 Content (io.swagger.v3.oas.models.media.Content)6 MediaType (io.swagger.v3.oas.models.media.MediaType)6 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 Operation (io.swagger.v3.oas.models.Operation)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)3 PathItem (io.swagger.v3.oas.models.PathItem)3 Link (io.swagger.v3.oas.models.links.Link)3 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)3