Search in sources :

Example 31 with InlineModelResolver

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

the class InlineModelResolverTest method testArbitraryObjectRequestBodyInline.

@Test
public void testArbitraryObjectRequestBodyInline() {
    OpenAPI swagger = new OpenAPI();
    Schema schema = new Schema();
    schema.addProperties("arbitrary", new ObjectSchema());
    swagger.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(schema))))));
    new InlineModelResolver().flatten(swagger);
    Operation operation = swagger.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema().get$ref() != null);
    Schema body = swagger.getComponents().getSchemas().get("hello_body");
    assertTrue(body instanceof Schema);
    Schema property = (Schema) body.getProperties().get("arbitrary");
    assertNotNull(property);
    assertTrue(property instanceof ObjectSchema);
}
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) 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 32 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineModelTestWithTitle.

@Test
public void resolveInlineModelTestWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());
    Schema schema = new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);
    openAPI.getComponents().addSchemas("User", schema);
    new InlineModelResolver().flatten(openAPI);
    Schema user = openAPI.getComponents().getSchemas().get("User");
    assertNotNull(user);
    Schema address = (Schema) user.getProperties().get("address");
    assertTrue(address.get$ref() != null);
    Schema userAddressTitle = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(userAddressTitle);
    assertNotNull(userAddressTitle.getProperties().get("city"));
    assertNotNull(userAddressTitle.getProperties().get("street"));
}
Also used : Components(io.swagger.v3.oas.models.Components) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) 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) ArrayList(java.util.ArrayList) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 33 with InlineModelResolver

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

the class InlineModelResolverTest method testSkipInlineMatchesTrue.

@Test
public void testSkipInlineMatchesTrue() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver(false, false, true);
    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(6, openAPI.getComponents().getSchemas().size());
}
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 34 with InlineModelResolver

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

the class InlineModelResolverTest method testArbitraryObjectResponseMapInline.

@Test
public void testArbitraryObjectResponseMapInline() {
    OpenAPI openAPI = new OpenAPI();
    Schema schema = new Schema();
    schema.setAdditionalProperties(new ObjectSchema());
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(schema)))))));
    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(property.getAdditionalProperties() instanceof Schema);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    Schema inlineProp = (Schema) property.getAdditionalProperties();
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
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) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 35 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineArrayModelWithTitle.

@Test
public void resolveInlineArrayModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("InnerUserTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());
    ArraySchema arraySchema = new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);
    openAPI.getComponents().addSchemas("User", arraySchema);
    new InlineModelResolver().flatten(openAPI);
    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    Schema user = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
Also used : Components(io.swagger.v3.oas.models.Components) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) 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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedList(java.util.LinkedList) 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