Skip to content

Commit 073c7d7

Browse files
fix: add explicit-object-schema flag
1 parent 3a4ad2e commit 073c7d7

File tree

6 files changed

+77
-13
lines changed

6 files changed

+77
-13
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/ParseOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ParseOptions {
2222
private List<String> remoteRefAllowList;
2323
private List<String> remoteRefBlockList;
2424
private boolean explicitStyleAndExplode = true;
25+
private boolean explicitObjectSchema = true;
2526

2627

2728
public boolean isResolve() {
@@ -179,4 +180,12 @@ public void setExplicitStyleAndExplode(boolean explicitStyleAndExplode) {
179180
this.explicitStyleAndExplode = explicitStyleAndExplode;
180181
}
181182

183+
public boolean isExplicitObjectSchema() {
184+
return explicitObjectSchema;
185+
}
186+
187+
public void setExplicitObjectSchema(boolean explicitObjectSchema) {
188+
this.explicitObjectSchema = explicitObjectSchema;
189+
}
190+
182191
}

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private SwaggerParseResult readResult(SwaggerDeserializationResult result, List<
104104
SwaggerParseResult out = convert(result);
105105
if (out != null && out.getOpenAPI() != null && options != null) {
106106
if (options.isResolveFully()) {
107-
new ResolverFully(options.isResolveCombinators()).resolveFully(out.getOpenAPI());
107+
new ResolverFully(options).resolveFully(out.getOpenAPI());
108108
}
109109
if (options.isFlatten()) {
110110
try {

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private SwaggerParseResult resolve(SwaggerParseResult result, List<Authorization
227227
dereferencer.dereference(dereferencerContext, dereferencers.iterator());
228228
}
229229
if (options.isResolveFully()) {
230-
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
230+
new ResolverFully(options).resolveFully(result.getOpenAPI());
231231
}
232232
} else {
233233
String msg = "Resolution of OAS 3.1 spec disabled by 'disableOas31Resolve' env variable";
@@ -239,7 +239,7 @@ private SwaggerParseResult resolve(SwaggerParseResult result, List<Authorization
239239
location, null, options);
240240
resolver.resolve(result);
241241
if (options.isResolveFully()) {
242-
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
242+
new ResolverFully(options).resolveFully(result.getOpenAPI());
243243
}
244244
}
245245

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.swagger.v3.oas.models.parameters.RequestBody;
1616
import io.swagger.v3.oas.models.responses.ApiResponse;
1717
import io.swagger.v3.oas.models.responses.ApiResponses;
18+
import io.swagger.v3.parser.core.models.ParseOptions;
1819
import io.swagger.v3.parser.models.RefFormat;
1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
@@ -38,14 +39,15 @@ public class ResolverFully {
3839

3940
private boolean aggregateCombinators;
4041

42+
private ParseOptions parseOptions;
4143

42-
43-
public ResolverFully() {
44-
this(true);
45-
}
46-
47-
public ResolverFully(boolean aggregateCombinators) {
48-
this.aggregateCombinators = aggregateCombinators;
44+
public ResolverFully(ParseOptions options) {
45+
if (options != null) {
46+
this.aggregateCombinators = options.isResolveCombinators();
47+
} else {
48+
this.aggregateCombinators = true;
49+
}
50+
this.parseOptions = options;
4951
}
5052

5153
private Map<String, Schema> schemas;
@@ -493,7 +495,7 @@ public Schema resolveSchema(Schema schema) {
493495
Schema property = updated.get(key);
494496

495497
if (property.getProperties() != model.getProperties()) {
496-
if (!hasSchemaType(property)) {
498+
if (!hasSchemaType(property) && parseOptions.isExplicitObjectSchema()) {
497499
if (SpecVersion.V30.equals(property.getSpecVersion())) {
498500
property.setType("object");
499501
} else {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.testng.Assert;
4646
import org.testng.annotations.AfterClass;
4747
import org.testng.annotations.BeforeClass;
48+
import org.testng.annotations.DataProvider;
4849
import org.testng.annotations.Test;
4950

5051
import java.io.File;
@@ -597,7 +598,7 @@ public void testSelfReferenceResolution()throws Exception {
597598
options.setResolveFully(true);
598599

599600
OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml,auths,options).getOpenAPI();
600-
ResolverFully resolverUtil = new ResolverFully();
601+
ResolverFully resolverUtil = new ResolverFully(options);
601602
resolverUtil.resolveFully(openAPI);
602603

603604

@@ -643,7 +644,7 @@ public void testIssue85() {
643644
options.setResolveFully(true);
644645

645646
OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml,auths,options).getOpenAPI();
646-
ResolverFully resolverUtil = new ResolverFully();
647+
ResolverFully resolverUtil = new ResolverFully(options);
647648
resolverUtil.resolveFully(openAPI);
648649
Parameter param = openAPI.getPaths().get("/test/method").getPost().getParameters().get(0);
649650

@@ -1475,5 +1476,29 @@ public void testResolveArraySchemaItemsNullPointerException() {
14751476
final OpenAPI output = new OpenAPIV3Parser().read(actualLocation, null, options);
14761477
new OpenAPIResolver(output, null, actualLocation).resolve();
14771478
}
1479+
1480+
@Test(dataProvider = "explicitObjectSchemaProvider")
1481+
public void testIssue2113(boolean explicitObjectSchema) {
1482+
ParseOptions options = new ParseOptions();
1483+
options.setResolve(true);
1484+
options.setResolveFully(true);
1485+
options.setExplicitObjectSchema(explicitObjectSchema);
1486+
1487+
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue_2113.yaml", auths, options).getOpenAPI();
1488+
ObjectSchema schema = (ObjectSchema) openAPI.getPaths().get("/foo").getPost().getRequestBody().getContent().get("application/json").getSchema();
1489+
if (explicitObjectSchema) {
1490+
assertEquals(schema.getProperties().get("goo").getType(), "object");
1491+
} else {
1492+
assertNull(schema.getProperties().get("goo").getType());
1493+
}
1494+
}
1495+
1496+
@DataProvider(name = "explicitObjectSchemaProvider")
1497+
public Object[][] explicitObjectSchemaProvider() {
1498+
return new Object[][] {
1499+
{ true },
1500+
{ false }
1501+
};
1502+
}
14781503

14791504
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Issue X
5+
servers:
6+
- url: http://petstore.swagger.io/api
7+
paths:
8+
/foo:
9+
post:
10+
requestBody:
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/ObjectType'
15+
required: true
16+
responses:
17+
200:
18+
description: ok
19+
components:
20+
schemas:
21+
ObjectType:
22+
type: object
23+
allOf:
24+
- $ref: "#/components/schemas/NoType"
25+
NoType:
26+
properties:
27+
goo:
28+
title: "Boo"

0 commit comments

Comments
 (0)