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