Search in sources :

Example 71 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testDiscriminatorObject.

@Test
void testDiscriminatorObject(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0.1'\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      type: object\n" + "      required:\n" + "      - pet_type\n" + "      properties:\n" + "        pet_type:\n" + "          type: string\n" + "      discriminator:\n" + "        propertyName: pet_type\n" + "        mapping:\n" + "          cachorro: Dog\n" + "    Cat:\n" + "      allOf:\n" + "      - $ref: '#/components/schemas/Pet'\n" + "      - type: object\n" + "        # all other properties specific to a `Cat`\n" + "        properties:\n" + "          name:\n" + "            type: string\n" + "    Dog:\n" + "      allOf:\n" + "      - $ref: '#/components/schemas/Pet'\n" + "      - type: object\n" + "        # all other properties specific to a `Dog`\n" + "        properties:\n" + "          bark:\n" + "            type: string\n" + "    Lizard:\n" + "      allOf:\n" + "      - $ref: '#/components/schemas/Pet'\n" + "      - type: object\n" + "        # all other properties specific to a `Lizard`\n" + "        properties:\n" + "          lovesRocks:\n" + "            type: boolean";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertEquals(result.getOpenAPI().getComponents().getSchemas().get("Pet").getDiscriminator().getPropertyName(), "pet_type");
    assertEquals(result.getOpenAPI().getComponents().getSchemas().get("Pet").getDiscriminator().getMapping().get("cachorro"), "Dog");
    assertTrue(messages.contains("attribute paths is missing"));
    assertTrue(messages.contains("attribute info is missing"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 72 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testIssue161.

@Test
public void testIssue161() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + "  /users:\n" + "    get:\n" + "      parameters:\n" + "        - in: query\n" + "          name: name\n" + "          required: false\n" + "          schema:\n" + "            type: string\n" + "            minLength: 10\n" + "            maxLength: 100\n" + "      responses:\n" + "        default:\n" + "          description: ok\n";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    Set<String> messages = new HashSet<>(result.getMessages());
    assertFalse(messages.contains("attribute paths.'/users'(get).[name].maxLength is unexpected"));
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 73 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testDeserializeWithEnumDiscriminator.

@Test
public void testDeserializeWithEnumDiscriminator() {
    String yaml = "openapi: 3.0.0\n" + "components:\n" + "  schemas:\n" + "    Animal:\n" + "      type: object\n" + "      discriminator:\n" + "        propertyName: petType\n" + "      description: |\n" + "        A basic `Animal` object which can extend to other animal types.\n" + "      required:\n" + "        - commonName\n" + "        - petType\n" + "      properties:\n" + "        commonName:\n" + "          description: the household name of the animal\n" + "          type: string\n" + "        petType:\n" + "          description: |\n" + "            The discriminator for the animal type.  It _must_\n" + "            match one of the concrete schemas by name (i.e. `Cat`)\n" + "            for proper deserialization\n" + "          enum:\n" + "            - cat\n" + "            - dog";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    Map<String, Schema> properties = result.getOpenAPI().getComponents().getSchemas().get("Animal").getProperties();
    assertTrue(properties.containsKey("commonName"));
    assertTrue(properties.containsKey("petType"));
    assertEquals(properties.get("petType").getType(), "string");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 74 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testStyleInvalid.

@Test
public void testStyleInvalid() {
    String json = "{" + "    \"openapi\": \"3.0.0\"," + "    \"info\": {" + "        \"title\": \"realize\"," + "        \"version\": \"0.0.0\"" + "    }," + "    \"paths\": {" + "        \"/realize/{param}\": {" + "            \"post\": {" + "                \"parameters\": [" + "                    {" + "                        \"name\": \"param\"," + "                        \"in\": \"path\"," + "" + "                        \"style\": \"DERP\"," + "                        \"required\": true," + "" + "                        \"schema\": {" + "                            \"type\": \"string\"," + "                            \"nullable\": false," + "                            \"minLength\": 1" + "                        }" + "                    }" + "                ]," + "                \"responses\": {" + "                    \"200\": {" + "                        \"description\": \"Success\"," + "                        \"content\": {" + "                            \"application/json\": {" + "                                \"schema\": {" + "                                    \"type\": \"object\"" + "                                }" + "                            }" + "                        }" + "                    }" + "                }" + "            }" + "        }" + "    }" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    assertTrue(result.getMessages().size() == 1);
    assertEquals(result.getMessages().get(0), "attribute paths.'/realize/{param}'(post).parameters.[param].style is not of type `StyleEnum`");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 75 with OpenAPIV3Parser

use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testBodyContent.

@Test
public void testBodyContent() {
    String json = "{" + "  \"openapi\": \"3.0.0\"," + "  \"info\": {" + "    \"title\": \"Operations\"," + "    \"version\": \"0.0.0\"" + "  }," + "  \"paths\": {" + "    \"/operations\": {" + "      \"post\": {" + "        \"requestBody\": {" + "            \"description\": \"Content empty\"," + "            \"content\": {" + "            }" + "        }," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }," + "      \"put\": {" + "        \"requestBody\": {" + "            \"description\": \"Content undefined\"" + "        }," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }" + "    }" + "  }" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    Operation post = result.getOpenAPI().getPaths().get("/operations").getPost();
    assertEquals(post.getRequestBody().getContent(), null, "Empty content");
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).requestBody.content with no media type is unsupported"), true, "Empty content error reported");
    Operation put = result.getOpenAPI().getPaths().get("/operations").getPut();
    assertEquals(put.getRequestBody().getContent(), null, "Empty content");
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(put).requestBody.content is missing"), true, "Missing content error reported");
    assertEquals(result.getMessages().size(), 2, "Messages");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)295 Test (org.testng.annotations.Test)279 OpenAPI (io.swagger.v3.oas.models.OpenAPI)219 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)184 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)169 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)83 Schema (io.swagger.v3.oas.models.media.Schema)83 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)80 StringSchema (io.swagger.v3.oas.models.media.StringSchema)77 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)76 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)75 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)66 MapSchema (io.swagger.v3.oas.models.media.MapSchema)63 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)25 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)25 DateSchema (io.swagger.v3.oas.models.media.DateSchema)24 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)24 Parameter (io.swagger.v3.oas.models.parameters.Parameter)23 HashSet (java.util.HashSet)22 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)17