Search in sources :

Example 1 with Tag

use of io.swagger.v3.oas.models.tags.Tag 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 2 with Tag

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

the class SchemaSerializationTest method serializeRefSchema3_1.

@Test
public void serializeRefSchema3_1() {
    OpenAPI openAPI = new OpenAPI().components(new Components().addSchemas("Pet", new Schema().addProperties("id", new Schema().type("integer")).addProperties("name", new Schema().type("string")).addProperties("tag", new Schema().type("string"))).addSchemas("AnotherPet", new Schema().title("Another Pet").description("Another Pet for petstore referencing Pet schema").$ref("#/components/schemas/Pet").addProperties("category", new Schema().type("string")).addProperties("photoUrl", new Schema().type("string"))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.0.1\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      properties:\n" + "        id: {}\n" + "        name: {}\n" + "        tag: {}\n" + "    AnotherPet:\n" + "      title: Another Pet\n" + "      properties:\n" + "        category: {}\n" + "        photoUrl: {}\n" + "      description: Another Pet for petstore referencing Pet schema\n" + "      $ref: '#/components/schemas/Pet'");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.1\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "        name:\n" + "          type: string\n" + "        tag:\n" + "          type: string\n" + "    AnotherPet:\n" + "      $ref: '#/components/schemas/Pet'");
}
Also used : Components(io.swagger.v3.oas.models.Components) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with Tag

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

the class SimpleBuilderTest method testBuilder.

@Test
public void testBuilder() throws Exception {
    // basic metadata
    OpenAPI oai = new OpenAPI().info(new Info().contact(new Contact().email("tony@eatbacon.org").name("Tony the Tam").url("https://foo.bar"))).externalDocs(new ExternalDocumentation().description("read more here").url("http://swagger.io")).addTagsItem(new Tag().name("funky dunky").description("all about neat things")).extensions(new HashMap<String, Object>() {

        {
            put("x-fancy-extension", "something");
        }
    });
    Map<String, Schema> schemas = new HashMap<>();
    schemas.put("StringSchema", new StringSchema().description("simple string schema").minLength(3).maxLength(100).example("it works"));
    schemas.put("IntegerSchema", new IntegerSchema().description("simple integer schema").multipleOf(new BigDecimal(3)).minimum(new BigDecimal(6)));
    oai.components(new Components().schemas(schemas));
    schemas.put("Address", new Schema().description("address object").addProperties("street", new StringSchema().description("the street number")).addProperties("city", new StringSchema().description("city")).addProperties("state", new StringSchema().description("state").minLength(2).maxLength(2)).addProperties("zip", new StringSchema().description("zip code").pattern("^\\d{5}(?:[-\\s]\\d{4})?$").minLength(2).maxLength(2)).addProperties("country", new StringSchema()._enum(new ArrayList<String>() {

        {
            this.add("US");
        }
    })).description("2-digit country code").minLength(2).maxLength(2));
    oai.paths(new Paths().addPathItem("/foo", new PathItem().description("the foo path").get(new Operation().addParametersItem(new QueryParameter().description("Records to skip").required(false).schema(new IntegerSchema())).responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it worked").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("#/components/schemas/Address")))).addLink("funky", new Link().operationId("getFunky")))))));
    System.out.println(writeJson(oai));
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Info(io.swagger.v3.oas.models.info.Info) BigDecimal(java.math.BigDecimal) Contact(io.swagger.v3.oas.models.info.Contact) Content(io.swagger.v3.oas.models.media.Content) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Example 4 with Tag

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

the class PetData method createPet.

static Pet createPet(long id, Category cat, String name, String[] urls, String[] tags, String status) {
    Pet pet = new Pet();
    pet.setId(id);
    pet.setCategory(cat);
    pet.setName(name);
    if (null != urls) {
        List<String> urlObjs = new ArrayList<String>();
        for (String urlString : urls) {
            urlObjs.add(urlString);
        }
        pet.setPhotoUrls(urlObjs);
    }
    List<Tag> tagObjs = new ArrayList<Tag>();
    int i = 0;
    if (null != tags) {
        for (String tagString : tags) {
            i = i + 1;
            Tag tag = new Tag();
            tag.setId(i);
            tag.setName(tagString);
            tagObjs.add(tag);
        }
    }
    pet.setTags(tagObjs);
    pet.setStatus(status);
    return pet;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.swagger.v3.plugin.maven.resources.model.Tag) Pet(io.swagger.v3.plugin.maven.resources.model.Pet)

Example 5 with Tag

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

the class PetData method findPetByTags.

public List<Pet> findPetByTags(String tags) {
    String[] tagList = tags.split(",");
    List<Pet> result = new ArrayList<Pet>();
    for (Pet pet : pets) {
        if (null != pet.getTags()) {
            for (Tag tag : pet.getTags()) {
                for (String tagListString : tagList) {
                    if (tagListString.equals(tag.getName())) {
                        result.add(pet);
                    }
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.swagger.v3.plugin.maven.resources.model.Tag) Pet(io.swagger.v3.plugin.maven.resources.model.Pet)

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