Search in sources :

Example 16 with InlineModelResolver

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

the class InlineModelResolverTest method testSchemaPropertiesBeingPassedToFlattenedModel.

@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
public void testSchemaPropertiesBeingPassedToFlattenedModel() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema address = new ObjectSchema();
    address.setDeprecated(false);
    address.setDescription("My address");
    address.setExclusiveMaximum(true);
    address.setExclusiveMinimum(true);
    address.setFormat("format");
    address.setMinLength(Integer.getInteger("10"));
    address.setMaximum(BigDecimal.valueOf(50));
    address.setMaxItems(Integer.getInteger("1"));
    address.setMaxLength(Integer.getInteger("100"));
    address.setMaxProperties(Integer.getInteger("1"));
    address.setMinimum(BigDecimal.ZERO);
    address.setMinItems(Integer.getInteger("0"));
    address.setMinLength(Integer.getInteger("10"));
    address.setMinProperties(Integer.getInteger("0"));
    address.setMultipleOf(BigDecimal.valueOf(2));
    address.setName("Address");
    address.setNullable(true);
    address.setPattern("%dd");
    address.setReadOnly(false);
    address.setTitle("my address");
    address.setUniqueItems(true);
    address.setWriteOnly(false);
    address.addProperties("city", new StringSchema());
    Schema user = new ObjectSchema();
    user.setTitle("InnerUserTitle");
    user.setDefault("default");
    user.setReadOnly(false);
    user.setDescription("user description");
    user.setName("user name");
    user.addProperties("address", address);
    openAPI.getComponents().addSchemas("User", user);
    new InlineModelResolver(true, true).flatten(openAPI);
    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ObjectSchema);
    Schema userAddress = openAPI.getComponents().getSchemas().get("MyAddress");
    assertNotNull(userAddress);
    assertEquals(userAddress.getDeprecated(), Boolean.FALSE);
    assertEquals(userAddress.getDescription(), "My address");
    assertEquals(userAddress.getExclusiveMaximum(), Boolean.TRUE);
    assertEquals(userAddress.getExclusiveMinimum(), Boolean.TRUE);
    assertEquals(userAddress.getFormat(), "format");
    assertEquals(userAddress.getMaximum(), BigDecimal.valueOf(50));
    assertEquals(userAddress.getMaxItems(), Integer.getInteger("1"));
    assertEquals(userAddress.getMaxLength(), Integer.getInteger("100"));
    assertEquals(userAddress.getMaxProperties(), Integer.getInteger("1"));
    assertEquals(userAddress.getMinimum(), BigDecimal.ZERO);
    assertEquals(userAddress.getMinItems(), Integer.getInteger("1"));
    assertEquals(userAddress.getMinLength(), Integer.getInteger("100"));
    assertEquals(userAddress.getMinProperties(), Integer.getInteger("0"));
    assertEquals(userAddress.getMultipleOf(), BigDecimal.valueOf(2));
    assertEquals(userAddress.getName(), "Address");
    assertEquals(userAddress.getNullable(), Boolean.TRUE);
    assertEquals(userAddress.getPattern(), "%dd");
    assertEquals(userAddress.getReadOnly(), Boolean.FALSE);
    assertEquals(userAddress.getTitle(), "my address");
    assertEquals(userAddress.getUniqueItems(), Boolean.TRUE);
    assertEquals(userAddress.getWriteOnly(), Boolean.FALSE);
}
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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 17 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineRequestBody_stripsDotsFromPath.

@Test
public void resolveInlineRequestBody_stripsDotsFromPath() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());
    Schema schema = new Schema();
    schema.addProperties("address", objectSchema);
    schema.addProperties("name", new StringSchema());
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);
    Content content = new Content();
    content.addMediaType("*/*", mediaType);
    RequestBody requestBody = new RequestBody();
    requestBody.setContent(content);
    Operation operation = new Operation();
    operation.setRequestBody(requestBody);
    PathItem pathItem = new PathItem();
    pathItem.setGet(operation);
    openAPI.path("/api/Cloud.Greet.Hello", pathItem);
    new InlineModelResolver(true, true).flatten(openAPI);
    Operation getOperation = openAPI.getPaths().get("/api/Cloud.Greet.Hello").getGet();
    RequestBody body = getOperation.getRequestBody();
    assertEquals("use dot as common word separator: as it occurs frequently on OData services", "#/components/schemas/ApiCloudGreetHelloBody", body.getContent().get("*/*").getSchema().get$ref());
    Schema bodySchema = openAPI.getComponents().getSchemas().get("ApiCloudGreetHelloBody");
    assertTrue(bodySchema instanceof Schema);
    assertNotNull(bodySchema.getProperties().get("address"));
}
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) 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 18 with InlineModelResolver

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

the class InlineModelResolverTest method testArbitraryRequestBody.

@Test
public void testArbitraryRequestBody() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema()))))));
    new InlineModelResolver().flatten(openAPI);
    Operation operation = openAPI.getPaths().get("/hello").getGet();
    RequestBody requestBody = operation.getRequestBody();
    assertTrue(requestBody.getContent().get("*/*").getSchema() instanceof Schema);
    Schema schema = requestBody.getContent().get("*/*").getSchema();
    assertNull(schema.getType());
}
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) 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 19 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineArrayResponse.

@Test
public void resolveInlineArrayResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ObjectSchema items = new ObjectSchema();
    items.addExtension("x-ext", "ext-items");
    items.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema().items(items);
    schema.addExtension("x-ext", "ext-prop");
    ApiResponse response = new ApiResponse();
    response.addExtension("x-foo", "bar");
    response.description("it works!");
    response.content(new Content().addMediaType("*/*", new MediaType().schema(schema)));
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", response))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse apiResponse = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(apiResponse);
    assertNotNull(apiResponse.getContent().get("*/*").getSchema());
    Schema responseProperty = apiResponse.getContent().get("*/*").getSchema();
    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);
    ArraySchema ap = (ArraySchema) responseProperty;
    assertEquals(1, ap.getExtensions().size());
    assertEquals("ext-prop", ap.getExtensions().get("x-ext"));
    Schema p = ap.getItems();
    assertNotNull(p);
    assertEquals("#/components/schemas/inline_response_200", p.get$ref());
    assertEquals(1, p.getExtensions().size());
    assertEquals("ext-items", p.getExtensions().get("x-ext"));
    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") 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) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 20 with InlineModelResolver

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

the class InlineModelResolverTest method resolveInlineModelTestWithoutTitle.

@Test
public void resolveInlineModelTestWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    Schema objectSchema = new ObjectSchema();
    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 userAddress = openAPI.getComponents().getSchemas().get("User_address");
    assertNotNull(userAddress);
    assertNotNull(userAddress.getProperties().get("city"));
    assertNotNull(userAddress.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)

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