use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testSkipInlineMatchesFalse.
@Test
public void testSkipInlineMatchesFalse() {
final OpenAPI openAPI = new OpenAPI();
final InlineModelResolver inlineModelResolver = new InlineModelResolver();
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(openAPI.getComponents().getSchemas().size(), 6);
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class OpenAPIV3Parser method resolve.
private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
try {
if (options != null) {
if (options.isResolve() || options.isResolveFully()) {
OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
resolver.resolve(result);
if (options.isResolveFully()) {
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
}
}
if (options.isFlatten()) {
final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
if (result.getOpenAPI() != null) {
inlineModelResolver.flatten(result.getOpenAPI());
}
}
}
} catch (Exception e) {
LOGGER.warn("Exception while resolving:", e);
// TODO verify if this change makes sense (adding resolve messages instead of replacing)
result.getMessages().add(e.getMessage());
// result.setMessages(Collections.singletonList(e.getMessage()));
}
return result;
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testInlineMapResponse.
@Test
public void testInlineMapResponse() throws Exception {
OpenAPI openAPI = new OpenAPI();
Schema schema = new Schema();
schema.setAdditionalProperties(new StringSchema());
schema.addExtension("x-ext", "ext-prop");
ApiResponse apiResponse = new ApiResponse();
apiResponse.description("it works!");
MediaType mediaType = new MediaType();
mediaType.setSchema(schema);
Content content = new Content();
content.addMediaType("*/*", mediaType);
apiResponse.setContent(content);
apiResponse.addExtension("x-foo", "bar");
ApiResponses apiResponses = new ApiResponses();
apiResponses.addApiResponse("200", apiResponse);
openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(apiResponses)));
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(openAPI.getComponents().getSchemas() == null);
assertEquals(1, property.getExtensions().size());
assertEquals("ext-prop", property.getExtensions().get("x-ext"));
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineArrayRequestBody.
@Test
public void resolveInlineArrayRequestBody() throws Exception {
OpenAPI openAPI = new OpenAPI();
ObjectSchema addressSchema = new ObjectSchema();
addressSchema.addProperties("street", new StringSchema());
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.addProperties("address", addressSchema);
ArraySchema arraySchema = new ArraySchema();
arraySchema.items(objectSchema);
openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema))))));
new InlineModelResolver().flatten(openAPI);
RequestBody body = openAPI.getPaths().get("/hello").getGet().getRequestBody();
Schema schema = body.getContent().get("*/*").getSchema();
assertTrue(schema instanceof ArraySchema);
ArraySchema am = (ArraySchema) schema;
Schema inner = am.getItems();
assertTrue(inner.get$ref() != null);
assertEquals("#/components/schemas/hello_body", inner.get$ref());
Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
assertNotNull(inline);
assertTrue(inline instanceof Schema);
Schema address = (Schema) inline.getProperties().get("address");
assertNotNull(address);
assertEquals("#/components/schemas/hello_address", address.get$ref());
Schema inlineProp = openAPI.getComponents().getSchemas().get("hello_address");
assertNotNull(inlineProp);
assertTrue(inlineProp instanceof Schema);
assertNotNull(inlineProp.getProperties().get("street"));
assertTrue(inlineProp.getProperties().get("street") instanceof StringSchema);
}
use of io.swagger.v3.parser.util.InlineModelResolver in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testBasicInput.
@Test
public void testBasicInput() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
Schema user = new Schema();
user.addProperties("name", new StringSchema());
openAPI.path("/foo/baz", new PathItem().post(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("User")))))));
openAPI.getComponents().addSchemas("User", user);
new InlineModelResolver().flatten(openAPI);
}
Aggregations