Search in sources :

Example 11 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class XmlModelTest method deserializeModel.

@Test(description = "it should deserialize a model")
public void deserializeModel() throws IOException {
    final String yaml = "---\n" + "type: \"object\"\n" + "properties:\n" + "  id:\n" + "    type: \"string\"\n" + "    xml:\n" + "      attribute: true\n" + "  name:\n" + "    type: \"string\"\n" + "    xml:\n" + "      name: \"renamed\"\n" + "  list:\n" + "    type: \"array\"\n" + "    items:\n" + "      type: \"string\"\n" + "  wrappedList:\n" + "    type: \"array\"\n" + "    xml:\n" + "      name: \"wrappedListItems\"\n" + "      wrapped: true\n" + "    items:\n" + "      type: \"string\"\n" + "  forcedElement:\n" + "    type: \"array\"\n" + "    items:\n" + "      type: \"string\"\n" + "xml:\n" + "  name: \"rootName\"";
    final Schema model = Yaml.mapper().readValue(yaml, Schema.class);
    final Schema wrappedList = (Schema) model.getProperties().get("wrappedList");
    assertNotNull(wrappedList);
    assertNotNull(wrappedList.getXml());
    assertEquals(wrappedList.getXml().getName(), "wrappedListItems");
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) Test(org.testng.annotations.Test)

Example 12 with Yaml

use of io.swagger.v3.core.util.Yaml 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 13 with Yaml

use of io.swagger.v3.core.util.Yaml 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)

Example 14 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class ParameterSerializationTest method serializeHeaderParameter.

@Test(description = "it should it should serialize a HeaderParameter")
public void serializeHeaderParameter() {
    final Parameter p = new HeaderParameter().schema(new StringSchema());
    final String json = "{\"in\":\"header\",\"schema\":{\"type\":\"string\"}}";
    SerializationMatchers.assertEqualsToJson(p, json);
    final String yaml = "---\n" + "in: \"header\"\n" + "schema:\n" + "  type: \"string\"";
    SerializationMatchers.assertEqualsToYaml(p, yaml);
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) Test(org.testng.annotations.Test)

Example 15 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class ParameterSerializationTest method serializeBodyParameterToYaml.

@Test(description = "it should serialize a BodyParameter to yaml")
public void serializeBodyParameterToYaml() {
    final Schema model = new Schema().title("Cat").addProperties("name", new StringSchema());
    final RequestBody p = new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(model)));
    final String yaml = "---\n" + "content:\n" + "  '*/*':\n" + "    schema:\n" + "      title: Cat\n" + "      properties:\n" + "        name:\n" + "          type: string";
    SerializationMatchers.assertEqualsToYaml(p, yaml);
}
Also used : Content(io.swagger.v3.oas.models.media.Content) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)63 OpenAPI (io.swagger.v3.oas.models.OpenAPI)55 Schema (io.swagger.v3.oas.models.media.Schema)15 Components (io.swagger.v3.oas.models.Components)13 Info (io.swagger.v3.oas.models.info.Info)13 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)9 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)8 SpecFilter (io.swagger.v3.core.filter.SpecFilter)8 StringSchema (io.swagger.v3.oas.models.media.StringSchema)8 AfterTest (org.testng.annotations.AfterTest)6 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)5 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)4 AbstractSpecFilter (io.swagger.v3.core.filter.AbstractSpecFilter)4 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)4 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 IOException (java.io.IOException)4 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3