Search in sources :

Example 76 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS31.

@Test
public void testDeserializationOnOAS31() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertEquals(openAPI.getInfo().getSummary(), "petstore sample for OAS 3.1.0");
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test");
    assertNotNull(openAPI.getWebhooks());
    assertFalse(openAPI.getWebhooks().isEmpty());
    assertNotNull(openAPI.getWebhooks().get("newPet"));
    assertNotNull(openAPI.getWebhooks().get("newPet").getPost());
    assertNotNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet"));
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getDescription(), "get a pet");
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet").getGet());
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getGet().getOperationId(), "getPet");
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions().get("x-test-extension"), "extended");
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertNull(openAPI.getInfo().getSummary());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 77 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS30.

@Test
public void testDeserializationOnOAS30() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertNull(openAPI.getInfo().getSummary());
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertNull(openAPI.getInfo().getLicense().getIdentifier());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 78 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class SchemaDeserializationTest method deserializeRefSchema3_1.

@Test
public void deserializeRefSchema3_1() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_refs_siblings.yaml");
    final OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("AnotherPet"));
    assertEquals(openAPI.getComponents().getSchemas().get("AnotherPet").get$ref(), "#/components/schemas/Pet");
    assertEquals(openAPI.getComponents().getSchemas().get("AnotherPet").getTitle(), "Another Pet");
    assertEquals(openAPI.getComponents().getSchemas().get("AnotherPet").getDescription(), "Another Pet for petstore referencing Pet schema");
    assertNotNull(openAPI.getComponents().getSchemas().get("AnotherPet").getProperties());
    assertNotNull(openAPI.getComponents().getSchemas().get("AnotherPet").getProperties().get("category"));
    assertNotNull(openAPI.getComponents().getSchemas().get("AnotherPet").getProperties().get("photoUrl"));
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 79 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class PatternAndSchemaPropertiesTest method testPatternAndSchemaProperties.

@Test
public void testPatternAndSchemaProperties() throws Exception {
    final ModelResolver modelResolver = new ModelResolver(mapper());
    ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
    Schema model = context.resolve(new AnnotatedType(AnnotatedPet.class));
    assertEquals(((Schema) model.getPatternProperties().get("what.*ever")).getFormat(), "int32");
    assertEquals(((Schema) model.getPatternProperties().get("it.*takes")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherCategory")).get$ref(), "#/components/schemas/Category");
    assertEquals(((Schema) model.getProperties().get("anotherInteger")).getFormat(), "int32");
    SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "AnnotatedPet:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  description: Annotated Pet\n" + "  nullable: true\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag");
    context.getDefinedModels().values().forEach(s -> new OpenAPISchema2JsonSchema().process(s));
    SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "AnnotatedPet:\n" + "  type:\n" + "  - object\n" + "  - \"null\"\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    category:\n" + "      $ref: '#/components/schemas/Category'\n" + "    name:\n" + "      type: string\n" + "    photoUrls:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        type: string\n" + "        xml:\n" + "          name: photoUrl\n" + "    tags:\n" + "      type: array\n" + "      xml:\n" + "        wrapped: true\n" + "      items:\n" + "        $ref: '#/components/schemas/Tag'\n" + "    status:\n" + "      type: string\n" + "      description: pet status in the store\n" + "      enum:\n" + "      - available\n" + "      - pending\n" + "      - sold\n" + "    anotherCategory:\n" + "      $ref: '#/components/schemas/Category'\n" + "    anotherInteger:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "  patternProperties:\n" + "    what.*ever:\n" + "      maximum: 10\n" + "      type: integer\n" + "      description: prop schema 1\n" + "      format: int32\n" + "    it.*takes:\n" + "      $ref: '#/components/schemas/Category'\n" + "  description: Annotated Pet\n" + "Category:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  description: prop schema 2\n" + "  xml:\n" + "    name: Category\n" + "Tag:\n" + "  type: object\n" + "  properties:\n" + "    id:\n" + "      type: integer\n" + "      format: int64\n" + "    name:\n" + "      type: string\n" + "  xml:\n" + "    name: Tag\n");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) OpenAPISchema2JsonSchema(io.swagger.v3.core.util.OpenAPISchema2JsonSchema) Schema(io.swagger.v3.oas.models.media.Schema) AnnotatedPet(io.swagger.v3.core.resolving.v31.model.AnnotatedPet) ModelConverterContextImpl(io.swagger.v3.core.converter.ModelConverterContextImpl) ModelResolver(io.swagger.v3.core.jackson.ModelResolver) Test(org.testng.annotations.Test)

Example 80 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class ModelSerializerTest method convertModel.

@Test(description = "it should convert a model")
public void convertModel() throws JsonProcessingException {
    final Schema pet = new Schema();
    final Map<String, Schema> props = new LinkedHashMap<String, Schema>();
    props.put("intValue", new IntegerSchema());
    props.put("longValue", new IntegerSchema().format("int64"));
    props.put("dateValue", new DateSchema());
    props.put("dateTimeValue", new DateTimeSchema());
    pet.setProperties(props);
    pet.setRequired(Arrays.asList("intValue", "name"));
    final String json = "{\n" + "   \"required\":[\n" + "      \"intValue\"\n" + "   ],\n" + "   \"properties\":{\n" + "      \"intValue\":{\n" + "         \"type\":\"integer\",\n" + "         \"format\":\"int32\"\n" + "      },\n" + "      \"longValue\":{\n" + "         \"type\":\"integer\",\n" + "         \"format\":\"int64\"\n" + "      },\n" + "      \"dateValue\":{\n" + "         \"type\":\"string\",\n" + "         \"format\":\"date\"\n" + "      },\n" + "      \"dateTimeValue\":{\n" + "         \"type\":\"string\",\n" + "         \"format\":\"date-time\"\n" + "      }\n" + "   }\n" + "}";
    SerializationMatchers.assertEqualsToJson(pet, json);
}
Also used : DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) 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) DateSchema(io.swagger.v3.oas.models.media.DateSchema) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)69 OpenAPI (io.swagger.v3.oas.models.OpenAPI)60 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 Schema (io.swagger.v3.oas.models.media.Schema)32 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)28 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)28 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)24 StringSchema (io.swagger.v3.oas.models.media.StringSchema)24 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 PathItem (io.swagger.v3.oas.models.PathItem)16 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)10 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)10 ArrayList (java.util.ArrayList)10 Operation (io.swagger.v3.oas.models.Operation)9 Paths (io.swagger.v3.oas.models.Paths)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8