Search in sources :

Example 11 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testSerializePetstore.

@Test
public void testSerializePetstore() throws Exception {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.yaml");
    final OpenAPI swagger = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(swagger);
    assertEquals(swagger.getInfo().getLicense().getIdentifier(), "test");
    SerializationMatchers.assertEqualsToYaml31(swagger, "openapi: 3.1.0\n" + "info:\n" + "  title: Swagger Petstore\n" + "  license:\n" + "    name: MIT\n" + "    identifier: test\n" + "  version: 1.0.0\n" + "servers:\n" + "- url: http://petstore.swagger.io/v1\n" + "paths:\n" + "  /pets:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: List all pets\n" + "      operationId: listPets\n" + "      parameters:\n" + "      - name: limit\n" + "        in: query\n" + "        description: How many items to return at one time (max 100)\n" + "        required: false\n" + "        schema:\n" + "          type: integer\n" + "          format: int32\n" + "      responses:\n" + "        \"200\":\n" + "          description: An paged array of pets\n" + "          headers:\n" + "            x-next:\n" + "              description: A link to the next page of responses\n" + "              schema:\n" + "                type: string\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "    post:\n" + "      tags:\n" + "      - pets\n" + "      summary: Create a pet\n" + "      operationId: createPets\n" + "      responses:\n" + "        \"201\":\n" + "          description: Null response\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "  /pets/{petId}:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: Info for a specific pet\n" + "      operationId: showPetById\n" + "      parameters:\n" + "      - name: petId\n" + "        in: path\n" + "        description: The id of the pet to retrieve\n" + "        required: true\n" + "        schema:\n" + "          type: string\n" + "      responses:\n" + "        \"200\":\n" + "          description: Expected response to a valid request\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      required:\n" + "      - id\n" + "      - name\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "        name:\n" + "          type:\n" + "          - string\n" + "          - integer\n" + "        tag:\n" + "          type: string\n" + "    Pets:\n" + "      type: array\n" + "      items:\n" + "        $ref: '#/components/schemas/Pet'\n" + "    Error:\n" + "      required:\n" + "      - code\n" + "      - message\n" + "      properties:\n" + "        code:\n" + "          type: integer\n" + "          format: int32\n" + "        message:\n" + "          type: string\n" + "webhooks:\n" + "  newPet:\n" + "    post:\n" + "      requestBody:\n" + "        description: Information about a new pet in the system\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/Pet'\n" + "      responses:\n" + "        \"200\":\n" + "          description: Return a 200 status to indicate that the data was received\n" + "            successfully");
    SerializationMatchers.assertEqualsToJson31(swagger, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"info\" : {\n" + "    \"title\" : \"Swagger Petstore\",\n" + "    \"license\" : {\n" + "      \"name\" : \"MIT\",\n" + "      \"identifier\" : \"test\"\n" + "    },\n" + "    \"version\" : \"1.0.0\"\n" + "  },\n" + "  \"servers\" : [ {\n" + "    \"url\" : \"http://petstore.swagger.io/v1\"\n" + "  } ],\n" + "  \"paths\" : {\n" + "    \"/pets\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"List all pets\",\n" + "        \"operationId\" : \"listPets\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"limit\",\n" + "          \"in\" : \"query\",\n" + "          \"description\" : \"How many items to return at one time (max 100)\",\n" + "          \"required\" : false,\n" + "          \"schema\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"An paged array of pets\",\n" + "            \"headers\" : {\n" + "              \"x-next\" : {\n" + "                \"description\" : \"A link to the next page of responses\",\n" + "                \"schema\" : {\n" + "                  \"type\" : \"string\"\n" + "                }\n" + "              }\n" + "            },\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      },\n" + "      \"post\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Create a pet\",\n" + "        \"operationId\" : \"createPets\",\n" + "        \"responses\" : {\n" + "          \"201\" : {\n" + "            \"description\" : \"Null response\"\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"/pets/{petId}\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Info for a specific pet\",\n" + "        \"operationId\" : \"showPetById\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"petId\",\n" + "          \"in\" : \"path\",\n" + "          \"description\" : \"The id of the pet to retrieve\",\n" + "          \"required\" : true,\n" + "          \"schema\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Expected response to a valid request\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"Pet\" : {\n" + "        \"required\" : [ \"id\", \"name\" ],\n" + "        \"properties\" : {\n" + "          \"id\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int64\"\n" + "          },\n" + "          \"name\" : {\n" + "            \"type\" : [\"string\", \"integer\"]\n" + "          },\n" + "          \"tag\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      },\n" + "      \"Pets\" : {\n" + "        \"type\" : \"array\",\n" + "        \"items\" : {\n" + "          \"$ref\" : \"#/components/schemas/Pet\"\n" + "        }\n" + "      },\n" + "      \"Error\" : {\n" + "        \"required\" : [ \"code\", \"message\" ],\n" + "        \"properties\" : {\n" + "          \"code\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          },\n" + "          \"message\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"webhooks\" : {\n" + "    \"newPet\" : {\n" + "      \"post\" : {\n" + "        \"requestBody\" : {\n" + "          \"description\" : \"Information about a new pet in the system\",\n" + "          \"content\" : {\n" + "            \"application/json\" : {\n" + "              \"schema\" : {\n" + "                \"$ref\" : \"#/components/schemas/Pet\"\n" + "              }\n" + "            }\n" + "          }\n" + "        },\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Return a 200 status to indicate that the data was received successfully\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 12 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class SecurityDefinitionTest method createModelWithSecurityRequirements.

@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final OpenAPI oas = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    oas.schemaRequirement("githubAccessCode", new SecurityScheme().flows(new OAuthFlows().authorizationCode(new OAuthFlow().scopes(new Scopes().addString("user:email", "Grants read access to a user’s email addresses.")))));
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id");
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person"))));
    final ApiResponse errorResponse = new ApiResponse().description("error response").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse)).addSecurityItem(new SecurityRequirement().addList("internal_oauth2", "user:email")).addSecurityItem(new SecurityRequirement().addList("api_key"));
    oas.path("/pets", new PathItem().get(get));
    final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
    SerializationMatchers.assertEqualsToJson(oas, json);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 13 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class ReaderTest method testGetTags.

@Test(description = "Get tags")
public void testGetTags() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(TagsResource.class);
    Operation operation = openAPI.getPaths().get("/").getGet();
    assertNotNull(operation);
    assertEquals(6, operation.getTags().size());
    assertEquals(operation.getTags().get(3), EXAMPLE_TAG);
    assertEquals(operation.getTags().get(1), SECOND_TAG);
    assertEquals(openAPI.getTags().get(1).getDescription(), "desc definition");
    assertEquals(openAPI.getTags().get(2).getExternalDocs().getDescription(), "docs desc");
}
Also used : Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 14 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class ReaderTest method testTicket3624.

@Test(description = "Optional handling")
public void testTicket3624() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Service.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /example/model:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve models for display to the user'\n" + "      operationId: getModels\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Response'\n" + "  /example/model/by/ids:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve models by their ids'\n" + "      operationId: getModelsById\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/ByIdResponse'\n" + "  /example/containerized/model:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve review insights for a specific product'\n" + "      operationId: getContainerizedModels\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/ContainerizedResponse'\n" + "components:\n" + "  schemas:\n" + "    Model:\n" + "      type: object\n" + "      properties:\n" + "        text:\n" + "          type: string\n" + "        title:\n" + "          type: string\n" + "        active:\n" + "          type: boolean\n" + "        schemaParent:\n" + "          $ref: '#/components/schemas/Model'\n" + "        optionalString:\n" + "          type: string\n" + "        parent:\n" + "          $ref: '#/components/schemas/Model'\n" + "        id:\n" + "          type: integer\n" + "          format: int32\n" + "    Response:\n" + "      type: object\n" + "      properties:\n" + "        count:\n" + "          type: integer\n" + "          format: int32\n" + "        models:\n" + "          type: array\n" + "          items:\n" + "            $ref: '#/components/schemas/Model'\n" + "    ByIdResponse:\n" + "      type: object\n" + "      properties:\n" + "        modelsById:\n" + "          type: object\n" + "          additionalProperties:\n" + "            $ref: '#/components/schemas/Model'\n" + "    ContainerizedResponse:\n" + "      type: object\n" + "      properties:\n" + "        totalCount:\n" + "          type: integer\n" + "          format: int32\n" + "        containerizedModels:\n" + "          type: array\n" + "          items:\n" + "            $ref: '#/components/schemas/ModelContainer'\n" + "    ModelContainer:\n" + "      type: object\n" + "      properties:\n" + "        text:\n" + "          type: string\n" + "        model:\n" + "          $ref: '#/components/schemas/Model'\n" + "        id:\n" + "          type: integer\n" + "          format: int32";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class ReaderTest method testTicket3694.

@Test(description = "overridden generic resource methods")
public void testTicket3694() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Ticket3694ResourceExtendedType.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /foo:\n" + "    post:\n" + "      tags:\n" + "      - Foo\n" + "      summary: Foo List in Interface\n" + "      operationId: foo\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}\n" + "  /bar:\n" + "    post:\n" + "      operationId: bar\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: string\n" + "  /another:\n" + "    post:\n" + "      operationId: another\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
    reader = new Reader(new OpenAPI());
    openAPI = reader.read(Ticket3694Resource.class);
    yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /foo:\n" + "    post:\n" + "      tags:\n" + "      - Foo\n" + "      summary: Foo List in Interface\n" + "      operationId: foo\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}\n" + "  /bar:\n" + "    post:\n" + "      operationId: bar\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: string\n" + "  /another:\n" + "    post:\n" + "      operationId: another\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
    reader = new Reader(new OpenAPI());
    openAPI = reader.read(Ticket3694ResourceSimple.class);
    yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /bar:\n" + "    post:\n" + "      operationId: bar\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: string";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
    reader = new Reader(new OpenAPI());
    openAPI = reader.read(Ticket3694ResourceSimpleSameReturn.class);
    yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /bar:\n" + "    post:\n" + "      operationId: bar\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: array\n" + "              items:\n" + "                type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : Ticket3694Resource(io.swagger.v3.jaxrs2.resources.generics.ticket3694.Ticket3694Resource) Ticket3694ResourceSimple(io.swagger.v3.jaxrs2.resources.generics.ticket3694.Ticket3694ResourceSimple) Ticket3694ResourceSimpleSameReturn(io.swagger.v3.jaxrs2.resources.generics.ticket3694.Ticket3694ResourceSimpleSameReturn) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)11 Operation (io.swagger.v3.oas.models.Operation)9 PathItem (io.swagger.v3.oas.models.PathItem)8 Schema (io.swagger.v3.oas.models.media.Schema)8 Tag (io.swagger.v3.oas.models.tags.Tag)8 Paths (io.swagger.v3.oas.models.Paths)7 MediaType (io.swagger.v3.oas.models.media.MediaType)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 Callback (io.swagger.v3.oas.models.callbacks.Callback)4 Content (io.swagger.v3.oas.models.media.Content)4 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)4 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)4 List (java.util.List)4 Map (java.util.Map)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3