Search in sources :

Example 6 with InlineModelResolver

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

the class InlineModelResolverTest method testSkipInlineMatchesFalse.

@Test
public void testSkipInlineMatchesFalse() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver();
    final Schema operationAlphaInAsset = new ObjectSchema();
    operationAlphaInAsset.setTitle("operationAlphaInAsset");
    operationAlphaInAsset.addProperties("id1", new IntegerSchema());
    operationAlphaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationAlphaIn = new ObjectSchema();
    operationAlphaIn.setTitle("operationAlphaIn");
    operationAlphaIn.addProperties("asset", operationAlphaInAsset);
    final Schema operationAlphaRequest = new ObjectSchema();
    operationAlphaRequest.setTitle("operationAlphaRequest");
    operationAlphaRequest.addProperties("in", operationAlphaIn);
    final Schema operationBetaInAsset = new ObjectSchema();
    operationBetaInAsset.setTitle("operationBetaInAsset");
    operationBetaInAsset.addProperties("id1", new IntegerSchema());
    operationBetaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationBetaIn = new ObjectSchema();
    operationBetaIn.setTitle("operationBetaIn");
    operationBetaIn.addProperties("asset", operationBetaInAsset);
    final Schema operationBetaRequest = new ObjectSchema();
    operationBetaRequest.setTitle("operationBetaRequest");
    operationBetaRequest.addProperties("in", operationBetaIn);
    openAPI.path("/operationAlpha", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationAlphaRequest))))));
    openAPI.path("/operationBeta", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationBetaRequest))))));
    inlineModelResolver.flatten(openAPI);
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 7 with InlineModelResolver

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

the class OpenAPIV3Parser method resolve.

private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
    try {
        if (options != null) {
            if (options.isResolve() || options.isResolveFully()) {
                OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
                resolver.resolve(result);
                if (options.isResolveFully()) {
                    new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
                }
            }
            if (options.isFlatten()) {
                final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
                if (result.getOpenAPI() != null) {
                    inlineModelResolver.flatten(result.getOpenAPI());
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Exception while resolving:", e);
        // TODO verify if this change makes sense (adding resolve messages instead of replacing)
        result.getMessages().add(e.getMessage());
    // result.setMessages(Collections.singletonList(e.getMessage()));
    }
    return result;
}
Also used : InlineModelResolver(io.swagger.v3.parser.util.InlineModelResolver) ReadContentException(io.swagger.v3.parser.exception.ReadContentException) EncodingNotSupportedException(io.swagger.v3.parser.exception.EncodingNotSupportedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResolverFully(io.swagger.v3.parser.util.ResolverFully)

Example 8 with InlineModelResolver

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

the class InlineModelResolverTest method testInlineMapResponse.

@Test
public void testInlineMapResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    Schema schema = new Schema();
    schema.setAdditionalProperties(new StringSchema());
    schema.addExtension("x-ext", "ext-prop");
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.description("it works!");
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);
    Content content = new Content();
    content.addMediaType("*/*", mediaType);
    apiResponse.setContent(content);
    apiResponse.addExtension("x-foo", "bar");
    ApiResponses apiResponses = new ApiResponses();
    apiResponses.addApiResponse("200", apiResponse);
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(apiResponses)));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 9 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineArrayRequestBody.

@Test
public void resolveInlineArrayRequestBody() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema addressSchema = new ObjectSchema();
    addressSchema.addProperties("street", new StringSchema());
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("address", addressSchema);
    ArraySchema arraySchema = new ArraySchema();
    arraySchema.items(objectSchema);
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema))))));
    new InlineModelResolver().flatten(openAPI);
    RequestBody body = openAPI.getPaths().get("/hello").getGet().getRequestBody();
    Schema schema = body.getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema am = (ArraySchema) schema;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);
    assertEquals("#/components/schemas/hello_body", inner.get$ref());
    Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    Schema address = (Schema) inline.getProperties().get("address");
    assertNotNull(address);
    assertEquals("#/components/schemas/hello_address", address.get$ref());
    Schema inlineProp = openAPI.getComponents().getSchemas().get("hello_address");
    assertNotNull(inlineProp);
    assertTrue(inlineProp instanceof Schema);
    assertNotNull(inlineProp.getProperties().get("street"));
    assertTrue(inlineProp.getProperties().get("street") instanceof StringSchema);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 10 with InlineModelResolver

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

the class InlineModelResolverTest method testBasicInput.

@Test
public void testBasicInput() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema user = new Schema();
    user.addProperties("name", new StringSchema());
    openAPI.path("/foo/baz", new PathItem().post(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("User")))))));
    openAPI.getComponents().addSchemas("User", user);
    new InlineModelResolver().flatten(openAPI);
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)35 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)35 Test (org.testng.annotations.Test)35 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)34 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)34 Schema (io.swagger.v3.oas.models.media.Schema)34 StringSchema (io.swagger.v3.oas.models.media.StringSchema)34 Operation (io.swagger.v3.oas.models.Operation)25 PathItem (io.swagger.v3.oas.models.PathItem)25 Content (io.swagger.v3.oas.models.media.Content)24 MediaType (io.swagger.v3.oas.models.media.MediaType)24 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)13 Components (io.swagger.v3.oas.models.Components)11 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)11 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)11 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)4 List (java.util.List)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1