Skip to content

Commit bb286a7

Browse files
fix: add explicit-object-schema flag (#2113) (#2202)
1 parent 3a4ad2e commit bb286a7

File tree

7 files changed

+94
-7
lines changed

7 files changed

+94
-7
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,21 @@ components:
654654
type: string
655655
example: "94022"
656656
```
657+
#### 5. explicitObjectSchema:
658+
659+
```java
660+
ParseOptions parseOptions = new ParseOptions();
661+
parseOptions.setResolve(true); // implicit
662+
parseOptions.setResolveFully(true);
663+
parseOptions.setResolveCombinators(false);
664+
parseOptions.setExplicitObjectSchema(false); // default is true
665+
final OpenAPI openAPI = new OpenAPIV3Parser().read("a.yaml", null, parseOptions);
666+
```
667+
668+
This option allows you to customize the processing of schema properties when the type is not specified. By default it is set to `true`.
669+
- `true` : when the type is not defined for property, ‘object’ is set.
670+
- `false` : the property remains undefined when no type is specified
671+
It's only applied when `resolveFully` is set to `true`.
657672

658673
### Extensions
659674
This project has a core artifact--`swagger-parser`, which uses Java Service Provider Interface (SPI) so additional extensions can be added.

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: 12 additions & 2 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,7 +39,7 @@ public class ResolverFully {
3839

3940
private boolean aggregateCombinators;
4041

41-
42+
private ParseOptions parseOptions = new ParseOptions();
4243

4344
public ResolverFully() {
4445
this(true);
@@ -48,6 +49,15 @@ public ResolverFully(boolean aggregateCombinators) {
4849
this.aggregateCombinators = aggregateCombinators;
4950
}
5051

52+
public ResolverFully(ParseOptions options) {
53+
if (options != null) {
54+
this.aggregateCombinators = options.isResolveCombinators();
55+
} else {
56+
this.aggregateCombinators = true;
57+
}
58+
this.parseOptions = options;
59+
}
60+
5161
private Map<String, Schema> schemas;
5262
private Map<String, Schema> resolvedModels = new HashMap<>();
5363
private Map<String, Example> examples;
@@ -493,7 +503,7 @@ public Schema resolveSchema(Schema schema) {
493503
Schema property = updated.get(key);
494504

495505
if (property.getProperties() != model.getProperties()) {
496-
if (!hasSchemaType(property)) {
506+
if (!hasSchemaType(property) && parseOptions.isExplicitObjectSchema()) {
497507
if (SpecVersion.V30.equals(property.getSpecVersion())) {
498508
property.setType("object");
499509
} 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)