use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineRequestBody.
@Test
public void resolveInlineRequestBody() 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("/hello", pathItem);
new InlineModelResolver().flatten(openAPI);
Operation getOperation = openAPI.getPaths().get("/hello").getGet();
RequestBody body = getOperation.getRequestBody();
assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);
Schema bodySchema = openAPI.getComponents().getSchemas().get("hello_body");
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 testArrayResponse.
@Test
public void testArrayResponse() {
OpenAPI openAPI = new OpenAPI();
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.addProperties("name", new StringSchema());
ArraySchema schema = new ArraySchema();
schema.setItems(objectSchema);
ApiResponse apiResponse = new ApiResponse();
apiResponse.addExtension("x-foo", "bar");
apiResponse.setDescription("it works!");
apiResponse.setContent(new Content().addMediaType("*/*", new MediaType().schema(schema)));
openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", apiResponse))));
new InlineModelResolver().flatten(openAPI);
ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);
ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema();
Schema items = am.getItems();
assertTrue(items.get$ref() != null);
assertEquals(items.get$ref(), "#/components/schemas/inline_response_200");
Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
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 resolveInlineModel2DifferentInnerModelsWIthSameTitle.
@Test
public void resolveInlineModel2DifferentInnerModelsWIthSameTitle() 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);
Schema addressSchema = new ObjectSchema();
addressSchema.setTitle("UserAddressTitle");
addressSchema.setDefault("default");
addressSchema.setReadOnly(false);
addressSchema.setDescription("description");
addressSchema.setName("name");
addressSchema.addProperties("street", new StringSchema());
addressSchema.addProperties("city", new StringSchema());
addressSchema.addProperties("apartment", new StringSchema());
Schema anotherSchema = new Schema();
anotherSchema.setName("AnotherUser");
anotherSchema.setDescription("a common user");
List<String> requiredFields = new ArrayList<>();
requiredFields.add("address");
anotherSchema.setRequired(requiredFields);
anotherSchema.addProperties("name", new StringSchema());
anotherSchema.addProperties("lastName", new StringSchema());
anotherSchema.addProperties("address", addressSchema);
openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);
new InlineModelResolver().flatten(openAPI);
Schema user = openAPI.getComponents().getSchemas().get("User");
assertNotNull(user);
Schema userAddress = (Schema) user.getProperties().get("address");
assertTrue(userAddress.get$ref() != null);
Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
assertNotNull(address);
assertNotNull(address.getProperties().get("city"));
assertNotNull(address.getProperties().get("street"));
Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_1");
assertNotNull(duplicateAddress);
assertNotNull(duplicateAddress.getProperties().get("city"));
assertNotNull(duplicateAddress.getProperties().get("street"));
assertNotNull(duplicateAddress.getProperties().get("apartment"));
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineModel2EqualInnerModels.
@Test
public void resolveInlineModel2EqualInnerModels() 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);
Schema addressSchema = new ObjectSchema();
addressSchema.setTitle("UserAddressTitle");
addressSchema.setDefault("default");
addressSchema.setReadOnly(false);
addressSchema.setDescription("description");
addressSchema.setName("name");
addressSchema.addProperties("street", new StringSchema());
addressSchema.addProperties("city", new StringSchema());
Schema anotherSchema = new Schema();
anotherSchema.setName("user");
anotherSchema.setDescription("a common user");
List<String> requiredFields = new ArrayList<>();
requiredFields.add("address");
anotherSchema.setRequired(requiredFields);
anotherSchema.addProperties("name", new StringSchema());
anotherSchema.addProperties("lastName", new StringSchema());
anotherSchema.addProperties("address", addressSchema);
openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);
new InlineModelResolver().flatten(openAPI);
Schema user = openAPI.getComponents().getSchemas().get("User");
assertNotNull(user);
Schema addressSchema1 = (Schema) user.getProperties().get("address");
assertTrue(addressSchema1.get$ref() != null);
Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
assertNotNull(address);
assertNotNull(address.getProperties().get("city"));
assertNotNull(address.getProperties().get("street"));
Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_0");
assertNull(duplicateAddress);
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testArbitraryObjectModelInline.
@Test
public void testArbitraryObjectModelInline() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
Schema userSchema = new Schema();
userSchema.setName("user");
userSchema.setDescription("a common user");
userSchema.addProperties("name", new StringSchema());
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.setTitle("title");
objectSchema.setDefault("default");
objectSchema.setReadOnly(false);
objectSchema.setDescription("description");
objectSchema.setName("name");
userSchema.addProperties("arbitrary", objectSchema);
List required = new ArrayList();
required.add("arbitrary");
userSchema.setRequired(required);
openAPI.getComponents().addSchemas("User", userSchema);
new InlineModelResolver().flatten(openAPI);
Schema user = openAPI.getComponents().getSchemas().get("User");
assertNotNull(user);
Schema inlineProp = (Schema) user.getProperties().get("arbitrary");
assertTrue(inlineProp instanceof ObjectSchema);
assertNull(inlineProp.getProperties());
}
Aggregations