Search in sources :

Example 21 with OpenAPIV3Parser

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

the class OpenAPIDeserializerTest method testOneOfSchema.

@Test
public void testOneOfSchema(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0'\n" + "components:\n" + "  schemas:\n" + "    Cat:\n" + "      type: object\n" + "      # all properties specific to a `Cat`\n" + "      properties:\n" + "        purring:\n" + "          type: string\n" + "    Dog:\n" + "      type: object\n" + "      # all properties specific to a `Dog`\n" + "      properties:\n" + "        bark:\n" + "          type: string\n" + "    Pet:\n" + "      oneOf: \n" + "       - $ref: '#/components/schemas/Cat'\n" + "       - $ref: '#/components/schemas/Dog'\n" + "       - type: object\n" + "         # neither a `Cat` nor a `Dog`\n" + "         properties:\n" + "           name:\n" + "             type: string\n";
    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);
    Schema petSchema = result.getOpenAPI().getComponents().getSchemas().get("Pet");
    assertTrue(petSchema != null);
    assertTrue(petSchema instanceof ComposedSchema);
    ComposedSchema petCompSchema = (ComposedSchema) petSchema;
    List<Schema> oneOfSchemas = petCompSchema.getOneOf();
    assertTrue(oneOfSchemas != null);
    assertEquals(oneOfSchemas.size(), 3);
    Schema refCatSchema = oneOfSchemas.get(0);
    assertTrue(refCatSchema != null);
    assertEquals(refCatSchema.get$ref(), "#/components/schemas/Cat");
    Schema refDogSchema = oneOfSchemas.get(1);
    assertTrue(refDogSchema != null);
    assertEquals(refDogSchema.get$ref(), "#/components/schemas/Dog");
    Schema otherSchema = oneOfSchemas.get(2);
    assertTrue(otherSchema != null);
    Schema nameProp = (Schema) otherSchema.getProperties().get("name");
    assertTrue(nameProp != null);
    assertEquals(nameProp.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) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 22 with OpenAPIV3Parser

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

the class OpenAPIDeserializerTest method testIssue1072.

@Test
public void testIssue1072() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "info:\n" + "  title: Test\n" + "  version: 1.0.0\n" + "\n" + "paths:\n" + "  /value:\n" + "    get:\n" + "      operationId: getValues\n" + "      responses:\n" + "        200:\n" + "          description: Successful response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/ComponentA'\n" + "components:\n" + "  schemas:\n" + "    ComponentA:\n" + "      description: Component A\n" + "      type: object\n" + "      allOf:\n" + "        - type: object\n" + "          properties:\n" + "            attributeWithoutType:\n" + "              allOf:\n" + "              - $ref: '#/components/schemas/ComponentB'\n" + "              default: \"coucou\"\n" + "            attributeWithWrongType:\n" + "              type: object\n" + "              allOf:\n" + "                - $ref: '#/components/schemas/ComponentB'\n" + "              default: \"coucou\"\n" + "            correctAttribute:\n" + "              type: string\n" + "              allOf:\n" + "                - $ref: '#/components/schemas/ComponentB'\n" + "              default: \"coucou\"\n" + "    ComponentB:\n" + "      description: Component B\n" + "      type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 23 with OpenAPIV3Parser

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

the class OpenAPIDeserializerTest method testParamContent.

@Test
public void testParamContent() {
    String json = "{" + "  \"openapi\": \"3.0.0\"," + "  \"info\": {" + "    \"title\": \"Operations\"," + "    \"version\": \"0.0.0\"" + "  }," + "  \"paths\": {" + "    \"/operations\": {" + "      \"post\": {" + "        \"parameters\": [" + "          {" + "            \"name\": \"param0\"," + "            \"in\": \"query\"," + "            \"content\": {" + "            }" + "          }," + "          {" + "            \"name\": \"param1\"," + "            \"in\": \"query\"," + "            \"content\": {" + "              \"text/plain\": {" + "              }" + "            }" + "          }," + "          {" + "            \"name\": \"param2\"," + "            \"in\": \"query\"," + "            \"content\": {" + "              \"text/plain\": {" + "              }," + "              \"application/json\": {" + "                \"schema\": {" + "                  \"type\": \"object\"" + "                }" + "              }" + "            }" + "          }" + "        ]," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }" + "    }" + "  }" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    Operation post = result.getOpenAPI().getPaths().get("/operations").getPost();
    Parameter param0 = post.getParameters().stream().filter(p -> "param0".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param0"));
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].content with no media type is unsupported"), true, "No media types error reported");
    assertEquals(param0.getContent(), null, "Empty content");
    Parameter param1 = post.getParameters().stream().filter(p -> "param1".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param1"));
    assertEquals(param1.getContent().size(), 1, "Valid content size");
    Parameter param2 = post.getParameters().stream().filter(p -> "param2".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param2"));
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param2].content with multiple media types is unsupported"), true, "Multiple media types error reported");
    assertEquals(param2.getContent(), null, "Content with multiple media types");
    assertEquals(result.getMessages().size(), 2, "Messages");
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) 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)

Example 24 with OpenAPIV3Parser

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

the class OpenAPIDeserializerTest method testDeserializeByteString.

@Test
public void testDeserializeByteString() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 0.0.0\n" + "  title: My Title\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      description: a test\n" + "      responses:\n" + "        '200':\n" + "          description: Successful response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: object\n" + "                properties:\n" + "                  bytes:\n" + "                    $ref: '#/components/schemas/ByteString'\n" + "components:\n" + "  schemas:\n" + "    ByteString:\n" + "      type: string\n" + "      format: byte\n" + "      default: W.T.F?\n" + "      enum:\n" + "        - VGhlIHdvcmxk\n" + "        - aXMgYWxs\n" + "        - dGhhdCBpcw==\n" + "        - dGhlIGNhc2U=\n" + "        - W.T.F?\n" + "";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
    Schema byteModel = resolved.getComponents().getSchemas().get("ByteString");
    assertTrue(byteModel instanceof ByteArraySchema);
    List<byte[]> byteValues = byteModel.getEnum();
    assertEquals(byteValues.size(), 4);
    assertEquals(new String(byteValues.get(0)), "The world");
    assertEquals(new String(byteValues.get(1)), "is all");
    assertEquals(new String(byteValues.get(2)), "that is");
    assertEquals(new String(byteValues.get(3)), "the case");
    assertEquals(byteModel.getDefault(), null);
    assertEquals(result.getMessages(), Arrays.asList("attribute components.schemas.ByteString.enum=`W.T.F?` is not of type `byte`", "attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 25 with OpenAPIV3Parser

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

the class OpenAPIDeserializerTest method testDefinitions.

@Test
public void testDefinitions() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: ''\n" + "  title: ''\n" + "paths: {}\n" + "components:\n" + "  schemas:\n" + "    invalid: true\n" + "    Person:\n" + "      required:\n" + "        - id\n" + "        - name\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "        name:\n" + "          type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute components.schemas.invalid is not of type `object`"));
    assertTrue(result.getOpenAPI().getComponents().getSchemas().get("Person") instanceof Schema);
    List<String> required = ((Schema) result.getOpenAPI().getComponents().getSchemas().get("Person")).getRequired();
    Set<String> requiredKeys = new HashSet<String>(required);
    assertTrue(requiredKeys.contains("id"));
    assertTrue(requiredKeys.contains("name"));
    assertTrue(requiredKeys.size() == 2);
}
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) HashSet(java.util.HashSet) 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