Search in sources :

Example 6 with Paths

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

the class Ticket2926Test method testExtensionsInMapDeserializeAndSerialize.

@Test
public void testExtensionsInMapDeserializeAndSerialize() throws Exception {
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  title: My title\n" + "  description: API under test\n" + "  version: 1.0.7\n" + "  x-info: test\n" + "servers:\n" + "- url: http://localhost:9999/api\n" + "  x-server: test\n" + "  description: desc\n" + "  variables: \n" + "    serVar: \n" + "      description: desc\n" + "      x-serverVariable: test\n" + "paths:\n" + "  /foo/bar:\n" + "    get:\n" + "      callbacks:\n" + "        /foo/bar:\n" + "          get:\n" + "            description: getoperation\n" + "          x-callback: test\n" + "      responses:\n" + "        default:\n" + "          description: it works!\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                title: inline_response_200\n" + "                type: object\n" + "                properties:\n" + "                  name:\n" + "                    type: string\n" + "              x-mediatype: test\n" + "          x-response: test\n" + "        x-responses: test\n" + "        x-responses-object: \n" + "          aaa: bbb\n" + "        x-responses-array: \n" + "          - aaa\n" + "          - bbb\n" + "      x-operation: test\n" + "    x-pathitem: test\n" + "  x-paths: test\n" + "x-openapi-object: \n" + "  aaa: bbb\n" + "x-openapi-array: \n" + "  - aaa\n" + "  - bbb\n" + "x-openapi: test";
    OpenAPI aa = Yaml.mapper().readValue(yaml, OpenAPI.class);
    SerializationMatchers.assertEqualsToYaml(aa, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 7 with Paths

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

the class PathsDeserializer method deserialize.

@Override
public Paths deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final ObjectMapper mapper;
    if (openapi31) {
        mapper = Json31.mapper();
    } else {
        mapper = Json.mapper();
    }
    Paths result = new Paths();
    JsonNode node = jp.getCodec().readTree(jp);
    ObjectNode objectNode = (ObjectNode) node;
    Map<String, Object> extensions = new LinkedHashMap<>();
    for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
        String childName = it.next();
        JsonNode child = objectNode.get(childName);
        // if name start with `x-` consider it an extesion
        if (childName.startsWith("x-")) {
            extensions.put(childName, mapper.convertValue(child, Object.class));
        } else {
            result.put(childName, mapper.convertValue(child, PathItem.class));
        }
    }
    if (!extensions.isEmpty()) {
        result.setExtensions(extensions);
    }
    return result;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Paths(io.swagger.v3.oas.models.Paths) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with Paths

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

the class OpenAPI3_1SerializationTest method testPathItemsRefSerialization.

@Test
public void testPathItemsRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/pathTest", new PathItem().$ref("#/components/pathItems/pathTest").description("This is a ref path item").summary("ref path item")).components(new Components().addPathItem("pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /pathTest:\n" + "    $ref: '#/components/pathItems/pathTest'\n" + "    description: This is a ref path item\n" + "    summary: ref path item\n" + "components:\n" + "  pathItems:\n" + "    pathTest:\n" + "      description: test path item\n" + "      get:\n" + "        operationId: testPathItem\n" + "        responses:\n" + "          \"200\":\n" + "            description: response description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/pathTest\" : {\n" + "      \"summary\" : \"ref path item\",\n" + "      \"description\" : \"This is a ref path item\",\n" + "      \"$ref\" : \"#/components/pathItems/pathTest\"\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"pathItems\" : {\n" + "      \"pathTest\" : {\n" + "        \"description\" : \"test path item\",\n" + "        \"get\" : {\n" + "          \"operationId\" : \"testPathItem\",\n" + "          \"responses\" : {\n" + "            \"200\" : {\n" + "              \"description\" : \"response description\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 9 with Paths

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

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

the class ParameterSerializationTest 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)

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