Search in sources :

Example 36 with Paths

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

the class ReaderTest method testResponseWithRef.

@Test(description = "Responses with ref")
public void testResponseWithRef() {
    Components components = new Components();
    components.addResponses("invalidJWT", new ApiResponse().description("when JWT token invalid/expired"));
    OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
    Reader reader = new Reader(oas);
    OpenAPI openAPI = reader.read(RefResponsesResource.class);
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  description: info\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      summary: Simple get operation\n" + "      description: Defines a simple get operation with no inputs and a complex output\n" + "        object\n" + "      operationId: getWithPayloadResponse\n" + "      responses:\n" + "        \"200\":\n" + "          description: voila!\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/SampleResponseSchema'\n" + "        default:\n" + "          description: boo\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/GenericError'\n" + "        \"401\":\n" + "          $ref: '#/components/responses/invalidJWT'\n" + "      deprecated: true\n" + "components:\n" + "  schemas:\n" + "    GenericError:\n" + "      type: object\n" + "    SampleResponseSchema:\n" + "      type: object\n" + "  responses:\n" + "    invalidJWT:\n" + "      description: when JWT token invalid/expired";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : Components(io.swagger.v3.oas.models.Components) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 37 with Paths

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

the class ReaderTest method testUserAnnotation.

@Test(description = "test user annotation")
public void testUserAnnotation() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(UserAnnotationResource.class);
    Paths paths = openAPI.getPaths();
    assertEquals(paths.size(), 1);
    PathItem pathItem = paths.get("/test/status");
    assertNotNull(pathItem);
    Operation operation = pathItem.getGet();
    assertNotNull(operation);
    assertTrue(operation.getTags().contains("test"));
    assertTrue(operation.getResponses().getDefault().getContent().keySet().contains("application/json"));
    Schema schema = operation.getResponses().getDefault().getContent().values().iterator().next().getSchema();
    assertNotNull(schema);
    assertEquals(schema.getType(), "string");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 38 with Paths

use of io.swagger.v3.oas.models.Paths 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)

Example 39 with Paths

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

the class ReaderTest method testMoreResponses.

@Test(description = "More Responses")
public void testMoreResponses() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(EnhancedResponsesResource.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      summary: Simple get operation\n" + "      description: Defines a simple get operation with no inputs and a complex output\n" + "        object\n" + "      operationId: getWithPayloadResponse\n" + "      responses:\n" + "        \"200\":\n" + "          description: voila!\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/SampleResponseSchema'\n" + "        \"404\":\n" + "          description: not found!\n" + "        \"400\":\n" + "          description: boo\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/GenericError'\n" + "      deprecated: true\n" + "components:\n" + "  schemas:\n" + "    GenericError:\n" + "      type: object\n" + "    SampleResponseSchema:\n" + "      type: object\n";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 40 with Paths

use of io.swagger.v3.oas.models.Paths 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)

Aggregations

Test (org.testng.annotations.Test)154 OpenAPI (io.swagger.v3.oas.models.OpenAPI)145 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)75 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)70 PathItem (io.swagger.v3.oas.models.PathItem)61 Operation (io.swagger.v3.oas.models.Operation)46 Paths (io.swagger.v3.oas.models.Paths)45 Schema (io.swagger.v3.oas.models.media.Schema)40 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)36 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)33 Components (io.swagger.v3.oas.models.Components)32 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)27 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)25 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)23 Parameter (io.swagger.v3.oas.models.parameters.Parameter)23 Info (io.swagger.v3.oas.models.info.Info)21 MapSchema (io.swagger.v3.oas.models.media.MapSchema)21 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)21 DateSchema (io.swagger.v3.oas.models.media.DateSchema)19