Search in sources :

Example 1 with Paths

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

the class ParameterDeSerializationTest method testIssue1765.

@Test(description = "should serialize correctly typed numeric enums")
public void testIssue1765() throws Exception {
    String yaml = "openapi: '3.0.1'\n" + "paths:\n" + "  /test:\n" + "    get:\n" + "      parameters:\n" + "      - name: \"days\"\n" + "        in: \"path\"\n" + "        required: true\n" + "        schema:\n" + "          type: \"integer\"\n" + "          format: \"int32\"\n" + "          enum:\n" + "          - 1\n" + "          - 2\n" + "          - 3\n" + "          - 4\n" + "          - 5\n" + "      responses:\n" + "        default:\n" + "          description: great";
    OpenAPI swagger = Yaml.mapper().readValue(yaml, OpenAPI.class);
    SerializationMatchers.assertEqualsToYaml(swagger, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 2 with Paths

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

the class JsonDeserializationTest method testNullExampleDeserialization.

@Test
public void testNullExampleDeserialization() throws Exception {
    String yamlNull = "openapi: 3.0.1\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      description: Operation Description\n" + "      operationId: operationId\n" + "components:\n" + "  schemas:\n" + "    UserStatus:\n" + "      type: string\n" + "      example: null\n";
    String yamlMissing = "openapi: 3.0.1\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      description: Operation Description\n" + "      operationId: operationId\n" + "components:\n" + "  schemas:\n" + "    UserStatus:\n" + "      type: string\n";
    OpenAPI oas = Yaml.mapper().readValue(yamlNull, OpenAPI.class);
    Yaml.prettyPrint(oas);
    assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
    assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
    oas = Yaml.mapper().readValue(yamlMissing, OpenAPI.class);
    Yaml.prettyPrint(oas);
    assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample());
    assertFalse(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag());
    Yaml.prettyPrint(oas);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with Paths

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

the class JsonDeserializationTest method testDeserializeAPathRef.

@Test
public void testDeserializeAPathRef() throws Exception {
    final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/pathRef.json", OpenAPI.class);
    final PathItem petPath = oas.getPaths().get("/pet");
    assertNotNull(petPath.get$ref());
    assertEquals(petPath.get$ref(), "http://my.company.com/paths/health.json");
    assertTrue(oas.getPaths().get("/user") instanceof PathItem);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 4 with Paths

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

the class JsonDeserializationTest method testNullEnumItem.

@Test(description = "Deserialize null enum item")
public void testNullEnumItem() throws Exception {
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      tags:\n" + "      - MyTag\n" + "      summary: Operation Summary\n" + "      description: Operation Description\n" + "      operationId: operationId\n" + "      parameters:\n" + "      - name: subscriptionId\n" + "        in: query\n" + "        schema:\n" + "          type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}\n" + "components:\n" + "  schemas:\n" + "    UserStatus:\n" + "      type: integer\n" + "      description: some int values with null\n" + "      format: int32\n" + "      enum:\n" + "      - 1\n" + "      - 2\n" + "      - null\n";
    OpenAPI oas = Yaml.mapper().readValue(yaml, OpenAPI.class);
    assertEquals(oas.getComponents().getSchemas().get("UserStatus").getEnum(), Arrays.asList(1, 2, null));
    yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      tags:\n" + "      - MyTag\n" + "      summary: Operation Summary\n" + "      description: Operation Description\n" + "      operationId: operationId\n" + "      parameters:\n" + "      - name: subscriptionId\n" + "        in: query\n" + "        schema:\n" + "          type: string\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}\n" + "components:\n" + "  schemas:\n" + "    UserStatus:\n" + "      type: string\n" + "      description: some int values with null\n" + "      enum:\n" + "      - 1\n" + "      - 2\n" + "      - null\n";
    oas = Yaml.mapper().readValue(yaml, OpenAPI.class);
    assertEquals(oas.getComponents().getSchemas().get("UserStatus").getEnum(), Arrays.asList("1", "2", null));
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 5 with Paths

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

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() 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 Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.addExtension("x-test2", map);
    info.addExtension("x-test", "value");
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
    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("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
    final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
    swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    Json.prettyPrint(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) 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) 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) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) 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