Search in sources :

Example 26 with Tag

use of io.swagger.v3.oas.models.tags.Tag in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testOperationBodyparamRemoteRefs.

private void testOperationBodyparamRemoteRefs(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref(remoteRef)))))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final RequestBody param = swagger.getPaths().get("/fun").getGet().getRequestBody();
    final Schema ref = param.getContent().get("*/*").getSchema();
    assertEquals(ref.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 27 with Tag

use of io.swagger.v3.oas.models.tags.Tag in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testPetstore.

@Test
public void testPetstore() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("src/test/resources/petstore.yaml", null, options);
    assertNotNull(result);
    assertTrue(result.getMessages().size() == 2);
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    Set<String> expectedDefinitions = new HashSet<String>();
    expectedDefinitions.add("User");
    expectedDefinitions.add("Category");
    expectedDefinitions.add("Pet");
    expectedDefinitions.add("Tag");
    expectedDefinitions.add("Order");
    expectedDefinitions.add("PetArray");
    assertEquals(definitions.keySet(), expectedDefinitions);
    Schema petModel = definitions.get("Pet");
    Set<String> expectedPetProps = new HashSet<String>();
    expectedPetProps.add("id");
    expectedPetProps.add("category");
    expectedPetProps.add("name");
    expectedPetProps.add("photoUrls");
    expectedPetProps.add("tags");
    expectedPetProps.add("status");
    assertEquals(petModel.getProperties().keySet(), expectedPetProps);
    ArraySchema petArrayModel = (ArraySchema) definitions.get("PetArray");
    assertEquals(petArrayModel.getType(), "array");
    Schema refProp = petArrayModel.getItems();
    assertEquals(refProp.get$ref(), "#/components/schemas/Pet");
    assertNull(petArrayModel.getProperties());
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 28 with Tag

use of io.swagger.v3.oas.models.tags.Tag in project flow by vaadin.

the class AbstractEndpointGenerationTest method assertPath.

private void assertPath(Class<?> testEndpointClass, Method expectedEndpointMethod, PathItem actualPath, HashMap<String, Class<?>> typeArguments) {
    Operation actualOperation = actualPath.getPost();
    assertEquals("Unexpected tag in the OpenAPI spec", Collections.singletonList(testEndpointClass.getSimpleName()), actualOperation.getTags());
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the endpoint name of the class '%s'", testEndpointClass.getSimpleName()), actualOperation.getOperationId().contains(getEndpointName(testEndpointClass)));
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the name of the endpoint method '%s'", expectedEndpointMethod.getName()), actualOperation.getOperationId().contains(expectedEndpointMethod.getName()));
    if (expectedEndpointMethod.getParameterCount() > 0) {
        Schema requestSchema = extractSchema(actualOperation.getRequestBody().getContent());
        Type[] genericParameterTypes = expectedEndpointMethod.getGenericParameterTypes();
        Class<?>[] parameterTypes = new Class<?>[genericParameterTypes.length];
        List<HashMap<String, Class<?>>> parameterTypeArguments = new ArrayList<>();
        for (int i = 0; i < genericParameterTypes.length; i++) {
            parameterTypes[i] = applyTypeArguments(genericParameterTypes[i], typeArguments);
            parameterTypeArguments.add(i, extractTypeArguments(genericParameterTypes[i], typeArguments));
        }
        assertRequestSchema(requestSchema, parameterTypes, parameterTypeArguments, expectedEndpointMethod.getParameters());
    } else {
        assertNull(String.format("No request body should be present in path schema for endpoint method with no parameters, method: '%s'", expectedEndpointMethod), actualOperation.getRequestBody());
    }
    ApiResponses responses = actualOperation.getResponses();
    assertEquals("Every operation is expected to have a single '200' response", 1, responses.size());
    ApiResponse apiResponse = responses.get("200");
    assertNotNull("Every operation is expected to have a single '200' response", apiResponse);
    Type genericReturnType = expectedEndpointMethod.getGenericReturnType();
    Class<?> returnType = applyTypeArguments(genericReturnType, typeArguments);
    Class<?> mappedType = new EndpointTransferMapper().getTransferType(returnType);
    if (mappedType != null) {
        returnType = mappedType;
    }
    if (returnType != void.class) {
        assertSchema(extractSchema(apiResponse.getContent()), returnType, extractTypeArguments(genericReturnType, typeArguments));
    } else {
        assertNull(String.format("No response is expected to be present for void method '%s'", expectedEndpointMethod), apiResponse.getContent());
    }
    assertNotNull("Non-anonymous endpoint method should have a security data defined for it in the schema", actualOperation.getSecurity());
}
Also used : HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Endpoint(dev.hilla.Endpoint) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) EndpointTransferMapper(dev.hilla.endpointransfermapper.EndpointTransferMapper) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 29 with Tag

use of io.swagger.v3.oas.models.tags.Tag in project swagger-core by swagger-api.

the class PatternAndSchemaPropertiesTest method testPatternAndSchemaProperties.

@Test
public void testPatternAndSchemaProperties() throws Exception {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    Schema model = context.resolve(new AnnotatedType(AnnotatedPet.class));
    assertEquals(((Schema) model.getPatternProperties().get("what.*ever")).getFormat(), "int32");
    assertEquals(((Schema) model.getPatternProperties().get("it.*takes")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherCategory")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherInteger")).getFormat(), "int32");
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "AnnotatedPet:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  description: Annotated Pet\n" + "  nullable: true\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag");
    context.getDefinedModels().values().forEach(s -> new OpenAPISchema2JsonSchema().process(s));
    SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "AnnotatedPet:\n" + "  type:\n" + "  - object\n" + "  - \"null\"\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  patternProperties:\n" + "    what.*ever:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "    it.*takes:\n" + "      $ref: '#/components/schemas/Category'\n" + "  description: Annotated Pet\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag\n");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) Schema(io.swagger.v3.oas.models.media.Schema) AnnotatedPet(io.swagger.v3.core.resolving.v31.model.AnnotatedPet) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 30 with Tag

use of io.swagger.v3.oas.models.tags.Tag in project swagger-core by swagger-api.

the class AnnotationsUtils method getTags.

public static Optional<Set<Tag>> getTags(io.swagger.v3.oas.annotations.tags.Tag[] tags, boolean skipOnlyName) {
    if (tags == null) {
        return Optional.empty();
    }
    Set<Tag> tagsList = new LinkedHashSet<>();
    for (io.swagger.v3.oas.annotations.tags.Tag tag : tags) {
        if (StringUtils.isBlank(tag.name())) {
            continue;
        }
        if (skipOnlyName && StringUtils.isBlank(tag.description()) && StringUtils.isBlank(tag.externalDocs().description()) && StringUtils.isBlank(tag.externalDocs().url())) {
            continue;
        }
        Tag tagObject = new Tag();
        if (StringUtils.isNotBlank(tag.description())) {
            tagObject.setDescription(tag.description());
        }
        tagObject.setName(tag.name());
        getExternalDocumentation(tag.externalDocs()).ifPresent(tagObject::setExternalDocs);
        if (tag.extensions().length > 0) {
            Map<String, Object> extensions = AnnotationsUtils.getExtensions(tag.extensions());
            if (extensions != null) {
                extensions.forEach(tagObject::addExtension);
            }
        }
        tagsList.add(tagObject);
    }
    if (tagsList.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(tagsList);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Tag(io.swagger.v3.oas.models.tags.Tag)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)18 Schema (io.swagger.v3.oas.models.media.Schema)14 ArrayList (java.util.ArrayList)13 Tag (io.swagger.v3.oas.models.tags.Tag)12 Test (org.testng.annotations.Test)12 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)11 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)11 StringSchema (io.swagger.v3.oas.models.media.StringSchema)11 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)10 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)10 Operation (io.swagger.v3.oas.models.Operation)9 Components (io.swagger.v3.oas.models.Components)7 PathItem (io.swagger.v3.oas.models.PathItem)7 OpenAPIResolver (io.swagger.v3.parser.OpenAPIResolver)7 MediaType (io.swagger.v3.oas.models.media.MediaType)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 Content (io.swagger.v3.oas.models.media.Content)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)5 HashMap (java.util.HashMap)5