Search in sources :

Example 6 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue 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 7 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testAlmostEmpty.

@Test
public void testAlmostEmpty(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0.1'\n" + "new: extra";
    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);
    assertTrue(messages.contains("attribute info is missing"));
    assertTrue(messages.contains("attribute paths is missing"));
    assertTrue(messages.contains("attribute new is unexpected"));
}
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 8 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testAnyOfSchema.

@Test
public void testAnyOfSchema(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0'\n" + "components:\n" + "  schemas:\n" + "    id:\n" + "      anyOf: \n" + "       - type: string\n" + "       - type: number\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 idSchema = result.getOpenAPI().getComponents().getSchemas().get("id");
    assertTrue(idSchema != null);
    assertTrue(idSchema instanceof ComposedSchema);
    ComposedSchema idCompSchema = (ComposedSchema) idSchema;
    List<Schema> anyOfSchemas = idCompSchema.getAnyOf();
    assertTrue(anyOfSchemas != null);
    assertEquals(anyOfSchemas.size(), 2);
    Schema stringSchema = anyOfSchemas.get(0);
    assertTrue(stringSchema != null);
    assertEquals(stringSchema.getType(), "string");
    Schema numberSchema = anyOfSchemas.get(1);
    assertTrue(numberSchema != null);
    assertEquals(numberSchema.getType(), "number");
}
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 9 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue 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 10 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrlTest method testAuthorizationHeaderWithNonMatchingUrl.

@Test
public void testAuthorizationHeaderWithNonMatchingUrl() throws Exception {
    final String expectedBody = setupStub();
    final String headerValue = "foobar";
    String authorization = "Authorization";
    final AuthorizationValue authorizationValue = new AuthorizationValue(authorization, headerValue, "header", u -> false);
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1")));
    assertEquals(1, requests.size());
    assertFalse(requests.get(0).containsHeader(authorization));
}
Also used : AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) LoggedRequest(com.github.tomakehurst.wiremock.verification.LoggedRequest) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)44 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)40 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)34 OpenAPI (io.swagger.v3.oas.models.OpenAPI)31 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)21 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)18 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)18 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 Schema (io.swagger.v3.oas.models.media.Schema)17 StringSchema (io.swagger.v3.oas.models.media.StringSchema)17 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)16 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)8 MapSchema (io.swagger.v3.oas.models.media.MapSchema)8 HashSet (java.util.HashSet)7 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)5 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)5 ArrayList (java.util.ArrayList)5 Expectations (mockit.Expectations)5