Search in sources :

Example 6 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.

the class AnchorTest method testBillionLaughProtectionSnakeYaml.

@Test
public void testBillionLaughProtectionSnakeYaml() {
    ParseOptions opts = new ParseOptions();
    opts.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("billion_laughs_snake_yaml.yaml", null, opts);
    assertNotNull(result.getOpenAPI().getComponents().getSchemas().get("a1"));
    assertEquals(result.getOpenAPI().getComponents().getSchemas().get("a1").getEnum().get(0), "AA1");
    assertNotNull(result.getOpenAPI().getComponents().getSchemas().get("c1"));
    assertEquals(((Schema) result.getOpenAPI().getComponents().getSchemas().get("c1").getProperties().get("a")).getEnum().get(0), "AA1");
    DeserializationUtils.getOptions().setMaxYamlAliasesForCollections(50);
    DeserializationUtils.getOptions().setYamlAllowRecursiveKeys(false);
    result = new OpenAPIV3Parser().readLocation("billion_laughs_snake_yaml.yaml", null, opts);
    DeserializationUtils.getOptions().setMaxYamlAliasesForCollections(Integer.MAX_VALUE);
    DeserializationUtils.getOptions().setYamlAllowRecursiveKeys(true);
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 7 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.

the class NetworkReferenceTest method testIssue411.

@Test
public void testIssue411() throws Exception {
    final List<AuthorizationValue> auths = new ArrayList<>();
    AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
    auths.add(auth);
    new Expectations() {

        {
            remoteUrl.urlToString("http://remote1/resources/swagger.yaml", auths);
            result = issue_411_server;
            remoteUrl.urlToString("http://remote2/resources/foo", auths);
            result = issue_411_components;
        }
    };
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/health"));
    PathItem health = swagger.getPaths().get("/health");
    assertTrue(health.getGet().getParameters().size() == 0);
    Schema responseRef = health.getGet().getResponses().get("200").getContent().get("*/*").getSchema();
    assertTrue(responseRef.get$ref() != null);
    assertEquals(responseRef.get$ref(), "#/components/schemas/Success");
    assertNotNull(swagger.getComponents().getSchemas().get("Success"));
    Parameter param = swagger.getPaths().get("/stuff").getGet().getParameters().get(0);
    assertEquals(param.getIn(), "query");
    assertEquals(param.getName(), "skip");
    ApiResponse response = swagger.getPaths().get("/stuff").getGet().getResponses().get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema() instanceof StringSchema);
    ApiResponse error = swagger.getPaths().get("/stuff").getGet().getResponses().get("400");
    assertNotNull(error);
    Schema errorProp = error.getContent().get("*/*").getSchema();
    assertNotNull(errorProp);
    assertTrue(errorProp.get$ref() != null);
    assertEquals(errorProp.get$ref(), "#/components/schemas/Error");
    assertTrue(swagger.getComponents().getSchemas().get("Error") instanceof Schema);
}
Also used : Expectations(mockit.Expectations) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) PathItem(io.swagger.v3.oas.models.PathItem) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 8 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.

the class FileReferenceTest method testIssue316.

@Test
public void testIssue316() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/nested-file-references/issue-316.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/events"));
    PathItem path = swagger.getPaths().get("/events");
    assertNotNull(path.getGet());
    Operation get = path.getGet();
    assertEquals(get.getOperationId(), "getEvents");
    assertTrue(swagger.getComponents().getSchemas().size() == 3);
    assertTrue(swagger.getComponents().getSchemas().get("Foobar").getProperties().size() == 1);
    assertTrue(swagger.getComponents().getSchemas().get("StatusResponse").getProperties().size() == 1);
    assertTrue(swagger.getComponents().getSchemas().get("Paging2").getProperties().size() == 2);
    Schema model = swagger.getComponents().getSchemas().get("Paging2");
    Schema property = (Schema) model.getProperties().get("foobar");
    assertTrue(property.get$ref() != null);
    assertEquals(property.get$ref(), "#/components/schemas/Foobar");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 9 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.

the class FileReferenceTest method testRelativeRefIssue421.

@Test
public void testRelativeRefIssue421() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/main.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger);
    assertNotNull(swagger.getPaths().get("pets"));
    assertNotNull(swagger.getPaths().get("pets").getGet());
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses());
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses().get("200"));
    assertNotNull(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema());
    assertTrue(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema().get$ref() != null);
    assertEquals(swagger.getPaths().get("pets").getGet().getResponses().get("200").getContent().get("*/*").getSchema().get$ref(), "#/components/schemas/Pet");
    assertTrue(swagger.getComponents().getSchemas().get("Pet") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Pet").getProperties().size() == 2);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) 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 10 with Schema

use of io.swagger.v3.oas.annotations.media.Schema in project swagger-parser by swagger-api.

the class FileReferenceTest method testIssue421.

@Test
public void testIssue421() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/nested-file-references/issue-421.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/pet/{petId}"));
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getGet().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getGet().getParameters().get(0).getName().equals("petId"));
    assertTrue(swagger.getComponents().getSchemas().get("Pet") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Pet").getProperties().size() == 6);
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost());
    assertNotNull(swagger.getPaths().get("/pet/{petId}").getPost().getParameters());
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getParameters().size() == 1);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody() != null);
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref() != null);
    assertEquals(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody");
    assertTrue(swagger.getPaths().get("/pet/{petId}").getPost().getRequestBody().get$ref().equals("#/components/requestBodies/requestBody"));
    assertNotNull(swagger.getPaths().get("/store/order"));
    assertNotNull(swagger.getPaths().get("/store/order").getPost());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody());
    assertNotNull(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema());
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref() != null);
    assertTrue(swagger.getPaths().get("/store/order").getPost().getRequestBody().getContent().get("application/json").getSchema().get$ref().equals("#/components/schemas/Order"));
    assertTrue(swagger.getComponents().getSchemas().get("Order") instanceof Schema);
    assertTrue(swagger.getComponents().getSchemas().get("Order").getProperties().size() == 6);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) 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)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)481 Test (org.testng.annotations.Test)464 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)370 StringSchema (io.swagger.v3.oas.models.media.StringSchema)311 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)258 OpenAPI (io.swagger.v3.oas.models.OpenAPI)250 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)246 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)239 MapSchema (io.swagger.v3.oas.models.media.MapSchema)170 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)116 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)99 DateSchema (io.swagger.v3.oas.models.media.DateSchema)96 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)86 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)86 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)84 Operation (io.swagger.v3.oas.models.Operation)72 MediaType (io.swagger.v3.oas.models.media.MediaType)72 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)70 Parameter (io.swagger.v3.oas.models.parameters.Parameter)69 Components (io.swagger.v3.oas.models.Components)67