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);
}
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"));
}
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());
}
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);
}
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"));
}
Aggregations