Skip to content

Commit d0ad044

Browse files
authored
Merge pull request #1380 from swagger-api/1303-refactor-v3-parser
Improvement for issue #1303
2 parents 6b9d848 + 3ef39f3 commit d0ad044

File tree

7 files changed

+193
-184
lines changed

7 files changed

+193
-184
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.swagger.v3.parser.core.models;
22

33
import io.swagger.v3.oas.models.OpenAPI;
4+
import java.util.Arrays;
5+
import java.util.Collections;
46
import java.util.List;
57

68
public class SwaggerParseResult {
@@ -27,4 +29,10 @@ public OpenAPI getOpenAPI() {
2729
public void setOpenAPI(OpenAPI openAPI) {
2830
this.openAPI = openAPI;
2931
}
32+
33+
public static SwaggerParseResult ofError(String message){
34+
final SwaggerParseResult result = new SwaggerParseResult();
35+
result.setMessages(Collections.singletonList(message));
36+
return result;
37+
}
3038
}

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

Lines changed: 146 additions & 169 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.swagger.v3.parser.exception;
2+
3+
public class EncodingNotSupportedException extends RuntimeException {
4+
private static final long serialVersionUID = 3686905713011188803L;
5+
6+
public EncodingNotSupportedException(String encoding) {
7+
super(String.format("Encoding `%s` is not supported by JRE", encoding));
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.swagger.v3.parser.exception;
2+
3+
/**
4+
* Happens when it's unable to read content from file or other resource
5+
*/
6+
public class ReadContentException extends RuntimeException {
7+
private static final long serialVersionUID = 4720926576862628428L;
8+
9+
public ReadContentException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,25 @@
2626

2727
public class InlineModelResolver {
2828
private OpenAPI openAPI;
29-
private boolean skipMatches;
3029
static Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
3130

3231
Map<String, Schema> addedModels = new HashMap<>();
3332
Map<String, String> generatedSignature = new HashMap<>();
3433

35-
private boolean flattenComposedSchemas;
36-
private boolean camelCaseFlattenNaming;
34+
private final boolean flattenComposedSchemas;
35+
private final boolean camelCaseFlattenNaming;
36+
private boolean skipMatches;
3737

38-
public InlineModelResolver(){this(false,false);}
38+
public InlineModelResolver(){this(false, false, false);}
3939

4040
public InlineModelResolver(boolean flattenComposedSchemas, boolean camelCaseFlattenNaming) {
41+
this(flattenComposedSchemas, camelCaseFlattenNaming, false);
42+
}
43+
44+
public InlineModelResolver(boolean flattenComposedSchemas, boolean camelCaseFlattenNaming, boolean skipMatches) {
4145
this.flattenComposedSchemas = flattenComposedSchemas;
4246
this.camelCaseFlattenNaming = camelCaseFlattenNaming;
47+
this.skipMatches = skipMatches;
4348
}
4449

4550
public void flatten(OpenAPI openAPI) {
@@ -319,7 +324,7 @@ private String resolveModelName(String title, String key) {
319324
}
320325

321326
public String matchGenerated(Schema model) {
322-
if (this.skipMatches) {
327+
if (skipMatches) {
323328
return null;
324329
}
325330
String json = Json.pretty(model);
@@ -585,17 +590,17 @@ public void copyVendorExtensions(Schema source, Schema target) {
585590
}
586591
}
587592

593+
private boolean isObjectSchema(Schema schema) {
594+
return schema instanceof ObjectSchema
595+
|| "object".equalsIgnoreCase(schema.getType())
596+
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
597+
}
598+
588599
public boolean isSkipMatches() {
589600
return skipMatches;
590601
}
591602

592603
public void setSkipMatches(boolean skipMatches) {
593604
this.skipMatches = skipMatches;
594605
}
595-
596-
private boolean isObjectSchema(Schema schema) {
597-
return schema instanceof ObjectSchema
598-
|| "object".equalsIgnoreCase(schema.getType())
599-
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
600-
}
601606
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ public void readingSpecNodeShouldNotOverQuotingStringExample() throws Exception
19241924
String yaml = Files.readFile(new File("src/test/resources/over-quoted-example.yaml"));
19251925
JsonNode rootNode = Yaml.mapper().readValue(yaml, JsonNode.class);
19261926
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1927-
OpenAPI openAPI = (parser.readWithInfo(null, rootNode)).getOpenAPI();
1927+
OpenAPI openAPI = (parser.parseJsonNode(null, rootNode)).getOpenAPI();
19281928

19291929
Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
19301930
assertEquals("NoQuotePlease", definitions.get("CustomerType").getExample());

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ public void testSkipInlineMatchesFalse() {
432432
final OpenAPI openAPI = new OpenAPI();
433433

434434
final InlineModelResolver inlineModelResolver = new InlineModelResolver();
435-
inlineModelResolver.setSkipMatches(false);
436435

437436
final Schema operationAlphaInAsset = new ObjectSchema();
438437
operationAlphaInAsset.setTitle("operationAlphaInAsset");
@@ -484,8 +483,7 @@ public void testSkipInlineMatchesFalse() {
484483
public void testSkipInlineMatchesTrue() {
485484
final OpenAPI openAPI = new OpenAPI();
486485

487-
final InlineModelResolver inlineModelResolver = new InlineModelResolver();
488-
inlineModelResolver.setSkipMatches(true);
486+
final InlineModelResolver inlineModelResolver = new InlineModelResolver(false, false, true);
489487

490488
final Schema operationAlphaInAsset = new ObjectSchema();
491489
operationAlphaInAsset.setTitle("operationAlphaInAsset");

0 commit comments

Comments
 (0)