Skip to content

swagger-api#1158: Provide the ability to toggle the match skipping for inline model resolving #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ParseOptions {
private boolean resolveCombinators = true;
private boolean resolveFully;
private boolean flatten;
private boolean skipMatches;

public boolean isResolve() {
return resolve;
Expand Down Expand Up @@ -33,4 +34,12 @@ public void setResolveFully(boolean resolveFully) {
public boolean isFlatten() { return flatten; }

public void setFlatten(boolean flatten) { this.flatten = flatten; }

public boolean isSkipMatches() {
return skipMatches;
}

public void setSkipMatches(boolean skipMatches) {
this.skipMatches = skipMatches;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auth
if (options.isResolveFully()) {
result.setOpenAPI(resolver.resolve());
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
}else if (options.isFlatten()){
InlineModelResolver inlineResolver = new InlineModelResolver();
inlineResolver.flatten(result.getOpenAPI());
} else if (options.isFlatten()) {
InlineModelResolver inlineModelResolver = new InlineModelResolver();
inlineModelResolver.setSkipMatches(options.isSkipMatches());
inlineModelResolver.flatten(result.getOpenAPI());
}
}
}
Expand Down Expand Up @@ -194,7 +195,9 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
result.setOpenAPI(new OpenAPIResolver(result.getOpenAPI(), auth, null).resolve());
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
} else if (options.isFlatten()) {
new InlineModelResolver().flatten(result.getOpenAPI());
InlineModelResolver inlineModelResolver = new InlineModelResolver();
inlineModelResolver.setSkipMatches(options.isSkipMatches());
inlineModelResolver.flatten(result.getOpenAPI());
}
}else{
JsonNode rootNode = mapper.readTree(swaggerAsString.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,48 @@ public void testResolveFullyMap() {
assertFalse(yaml.contains("$ref"));
}

@Test
public void testParseOptionsSkipMatchesFalse() {
final String location = "src/test/resources/skipMatches.yaml";

final ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
options.setSkipMatches(false);

final OpenAPIV3Parser parserUnderTest = new OpenAPIV3Parser();

final SwaggerParseResult result = parserUnderTest.readLocation(location, null, options);

final OpenAPI openAPI = result.getOpenAPI();

assertNotNull(openAPI);
assertNotNull(openAPI.getComponents());
assertNotNull(openAPI.getComponents().getSchemas());
assertEquals(4, openAPI.getComponents().getSchemas().size());
}

@Test
public void testParseOptionsSkipMatchesTrue() {
final String location = "src/test/resources/skipMatches.yaml";

final ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
options.setSkipMatches(true);

final OpenAPIV3Parser parserUnderTest = new OpenAPIV3Parser();

final SwaggerParseResult result = parserUnderTest.readLocation(location, null, options);

final OpenAPI openAPI = result.getOpenAPI();

assertNotNull(openAPI);
assertNotNull(openAPI.getComponents());
assertNotNull(openAPI.getComponents().getSchemas());
assertEquals(6, openAPI.getComponents().getSchemas().size());
}

private static int getDynamicPort() {
return new Random().ints(10000, 20000).findFirst().getAsInt();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -426,6 +427,111 @@ public void testInlineResponseModelWithTitle() throws Exception {
assertTrue(model.getProperties().get("name") instanceof StringSchema);
}

@Test
public void testSkipInlineMatchesFalse() {
final OpenAPI openAPI = new OpenAPI();

final InlineModelResolver inlineModelResolver = new InlineModelResolver();
inlineModelResolver.setSkipMatches(false);

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(4, openAPI.getComponents().getSchemas().size());
}

@Test
public void testSkipInlineMatchesTrue() {
final OpenAPI openAPI = new OpenAPI();

final InlineModelResolver inlineModelResolver = new InlineModelResolver();
inlineModelResolver.setSkipMatches(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());
}

@Test
public void resolveInlineArrayModelWithTitle() throws Exception {
Expand Down
48 changes: 48 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/skipMatches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: "3.0.0"
paths:
/operationAlpha:
get:
requestBody:
content:
application/json:
schema:
type: "object"
title: "operationAlphaRequest"
properties:
in:
type: "object"
title: "operationAlphaIn"
properties:
asset:
type: "object"
title: "operationAlphaInAsset"
properties:
id1:
type: "integer"
format: "int32"
id2:
type: "integer"
format: "int32"
/operationBeta:
get:
requestBody:
content:
application/json:
schema:
type: "object"
title: "operationBetaRequest"
properties:
in:
type: "object"
title: "operationBetaIn"
properties:
asset:
type: "object"
title: "operationBetaInAsset"
properties:
id1:
type: "integer"
format: "int32"
id2:
type: "integer"
format: "int32"