Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 9c163f3

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2.0-OpenAPITools
2 parents 591d27b + b49f9ee commit 9c163f3

20 files changed

Lines changed: 2151 additions & 1382 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import java.net.URI;
27+
import java.util.Collection;
2728
import java.util.LinkedHashMap;
2829
import java.util.Map;
2930

@@ -177,37 +178,40 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
177178
return newRef;
178179
}
179180

180-
private void processProperties(Map<String,Schema> subProps, String file) {
181-
if (subProps != null) {
182-
for (Map.Entry<String, Schema> prop : subProps.entrySet()) {
183-
if (prop.getValue().get$ref() != null) {
184-
processRefSchema(prop.getValue(), file);
185-
} else if (prop.getValue() instanceof ArraySchema) {
186-
ArraySchema arrayProp = (ArraySchema) prop.getValue();
187-
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null &&
188-
StringUtils.isNotBlank(arrayProp.getItems().get$ref())) {
189-
processRefSchema(arrayProp.getItems(), file);
190-
}
191-
if (arrayProp.getItems() != null && arrayProp.getItems().getProperties() != null ) {
192-
processProperties(arrayProp.getItems().getProperties(), file);
193-
}
194-
} else if (prop.getValue().getAdditionalProperties() != null && prop.getValue().getAdditionalProperties() instanceof Schema) {
195-
Schema mapProp = (Schema) prop.getValue().getAdditionalProperties();
196-
if (mapProp.get$ref() != null) {
197-
processRefSchema(mapProp, file);
198-
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema &&
199-
((ArraySchema) mapProp.getAdditionalProperties()).getItems()!= null &&
200-
((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref() != null
201-
&& StringUtils.isNotBlank(((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref())) {
202-
processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
203-
}
204-
}else if (prop.getValue() instanceof ObjectSchema){
205-
ObjectSchema objProp = (ObjectSchema) prop.getValue();
206-
if(objProp.getProperties() != null ){
207-
processProperties(objProp.getProperties(),file);
208-
}
209-
}
181+
private void processProperty(Schema property, String file) {
182+
if (property != null) {
183+
if (StringUtils.isNotBlank(property.get$ref())) {
184+
processRefSchema(property, file);
185+
}
186+
if (property.getProperties() != null) {
187+
processProperties(property.getProperties(), file);
210188
}
189+
if (property instanceof ArraySchema) {
190+
processProperty(((ArraySchema) property).getItems(), file);
191+
}
192+
if (property.getAdditionalProperties() instanceof Schema) {
193+
processProperty(((Schema) property.getAdditionalProperties()), file);
194+
}
195+
if (property instanceof ComposedSchema) {
196+
ComposedSchema composed = (ComposedSchema) property;
197+
processProperties(composed.getAllOf(), file);
198+
processProperties(composed.getAnyOf(), file);
199+
processProperties(composed.getOneOf(), file);
200+
}
201+
}
202+
}
203+
204+
private void processProperties(Collection<Schema> properties, String file) {
205+
if (properties != null) {
206+
for (Schema property : properties) {
207+
processProperty(property, file);
208+
}
209+
}
210+
}
211+
212+
private void processProperties(Map<String, Schema> properties, String file) {
213+
if (properties != null) {
214+
processProperties(properties.values(), file);
211215
}
212216
}
213217

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ParameterProcessor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public List<Parameter> processParameters(List<Parameter> parameters) {
8383
if (parameter.get$ref() != null) {
8484
RefFormat refFormat = computeRefFormat(parameter.get$ref());
8585
final Parameter resolvedParameter = cache.loadRef(parameter.get$ref(), refFormat, Parameter.class);
86+
if (parameter.get$ref().startsWith("#") && parameter.get$ref().indexOf("#/components/parameters") <= -1) {
87+
//TODO: Not possible to add warning during resolve doesn't accept result as an input. Hence commented below line.
88+
//result.warning(location, "The parameter should use Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.");
89+
continue;
90+
}
8691

8792
if(resolvedParameter == null) {
8893
// can't resolve it!
@@ -92,7 +97,7 @@ public List<Parameter> processParameters(List<Parameter> parameters) {
9297
// if the parameter exists, replace it
9398
boolean matched = false;
9499
for(Parameter param : processedPathLevelParameters) {
95-
if(param.getName().equals(resolvedParameter.getName())) {
100+
if(param != null && param.getName() != null && param.getName().equals(resolvedParameter.getName())) {
96101
// ref param wins
97102
matched = true;
98103
break;

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class OpenAPIDeserializer {
9090
private static final String COOKIE_PARAMETER = "cookie";
9191
private static final String PATH_PARAMETER = "path";
9292
private static final String HEADER_PARAMETER = "header";
93-
93+
private Components components;
9494
private final Set<String> operationIDs = new HashSet<>();
9595

9696
public SwaggerParseResult deserialize(JsonNode rootNode) {
@@ -134,17 +134,19 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
134134
openAPI.setInfo(info);
135135
}
136136

137+
obj = getObject("components", rootNode, false, location, result);
138+
if (obj != null) {
139+
Components components = getComponents(obj, "components", result);
140+
openAPI.setComponents(components);
141+
this.components=components;
142+
}
143+
137144
obj = getObject("paths", rootNode, true, location, result);
138145
if (obj != null) {
139146
Paths paths = getPaths(obj, "paths", result);
140147
openAPI.setPaths(paths);
141148
}
142149

143-
obj = getObject("components", rootNode, false, location, result);
144-
if (obj != null) {
145-
Components components = getComponents(obj, "components", result);
146-
openAPI.setComponents(components);
147-
}
148150

149151
ArrayNode array = getArray("servers", rootNode, false, location, result);
150152
if (array != null && array.size() > 0) {
@@ -558,7 +560,7 @@ private boolean isPathParamDefined(String pathParam, List<Parameter> parameters)
558560
return false;
559561
} else {
560562
Parameter pathParamDefined = parameters.stream()
561-
.filter(parameter -> pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn()))
563+
.filter(parameter -> (parameter.get$ref() != null) || (pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn())))
562564
.findFirst()
563565
.orElse(null);
564566
if (pathParamDefined == null) {
@@ -2040,6 +2042,25 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
20402042
JsonNode ref = node.get("$ref");
20412043
if (ref != null) {
20422044
if (ref.getNodeType().equals(JsonNodeType.STRING)) {
2045+
2046+
if(location.startsWith("paths")){
2047+
try{
2048+
String components[]=ref.asText().split("#/components");
2049+
if((ref.asText().startsWith("#/components"))&&(components.length>1)) {
2050+
String[] childComponents = components[1].split("/");
2051+
String[] newChildComponents = Arrays.copyOfRange(childComponents, 1, childComponents.length);
2052+
boolean isValidComponent = ReferenceValidator.valueOf(newChildComponents[0])
2053+
.validateComponent(this.components,
2054+
newChildComponents[1]);
2055+
if (!isValidComponent) {
2056+
result.missing(location, ref.asText());
2057+
}
2058+
}
2059+
}catch (Exception e){
2060+
result.missing(location, ref.asText());
2061+
}
2062+
}
2063+
20432064
String mungedRef = mungedRef(ref.textValue());
20442065
if (mungedRef != null) {
20452066
schema.set$ref(mungedRef);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.swagger.v3.parser.util;
2+
3+
import io.swagger.v3.oas.models.Components;
4+
5+
6+
public enum ReferenceValidator {
7+
8+
schemas {
9+
@Override
10+
public boolean validateComponent(Components components,String reference) {
11+
return components.getSchemas().containsKey(reference);
12+
}
13+
},
14+
responses {
15+
@Override
16+
public boolean validateComponent(Components components,String reference) {
17+
return components.getResponses().containsKey(reference);
18+
}
19+
},
20+
parameters {
21+
@Override
22+
public boolean validateComponent(Components components,String reference) {
23+
return components.getParameters().containsKey(reference);
24+
}
25+
},
26+
examples {
27+
@Override
28+
public boolean validateComponent(Components components,String reference) {
29+
return components.getExamples().containsKey(reference);
30+
}
31+
},
32+
requestBodies {
33+
@Override
34+
public boolean validateComponent(Components components,String reference) {
35+
return components.getRequestBodies().containsKey(reference);
36+
}
37+
},
38+
headers {
39+
@Override
40+
public boolean validateComponent(Components components,String reference) {
41+
return components.getHeaders().containsKey(reference);
42+
}
43+
},
44+
securitySchemes {
45+
@Override
46+
public boolean validateComponent(Components components,String reference) {
47+
return components.getSecuritySchemes().containsKey(reference);
48+
}
49+
},
50+
links {
51+
@Override
52+
public boolean validateComponent(Components components,String reference) {
53+
return components.getLinks().containsKey(reference);
54+
}
55+
},
56+
callbacks {
57+
@Override
58+
public boolean validateComponent(Components components,String reference) {
59+
return components.getCallbacks().containsKey(reference);
60+
}
61+
};
62+
63+
64+
public abstract boolean validateComponent(Components components,String reference);
65+
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,25 @@ public static Schema createSchemaByType(ObjectNode node){
5656
public static Schema createSchema(String type, String format) {
5757

5858
if(INTEGER_TYPE.equals(type)) {
59-
if(INTEGER64_FORMAT.equals(format)) {
60-
return new IntegerSchema().format(INTEGER64_FORMAT);
61-
}
62-
else {
63-
return new IntegerSchema();
59+
if(StringUtils.isBlank(format)){
60+
return new IntegerSchema().format(null);
61+
}else {
62+
return new IntegerSchema().format(format);
6463
}
6564
}
6665
else if(NUMBER_TYPE.equals(type)) {
67-
if(FLOAT_FORMAT.equals(format)) {
68-
return new NumberSchema().format(FLOAT_FORMAT);
69-
}
70-
else if(DOUBLE_FORMAT.equals(format)) {
71-
return new NumberSchema().format(DOUBLE_FORMAT);
72-
}
73-
else {
66+
if (StringUtils.isBlank(format)){
7467
return new NumberSchema();
68+
} else {
69+
return new NumberSchema().format(format);
7570
}
7671
}
7772
else if(BOOLEAN_TYPE.equals(type)) {
78-
return new BooleanSchema();
73+
if (StringUtils.isBlank(format)){
74+
return new BooleanSchema();
75+
} else {
76+
return new BooleanSchema().format(format);
77+
}
7978
}
8079
else if(STRING_TYPE.equals(type)) {
8180
if(BYTE_FORMAT.equals(format)) {
@@ -100,7 +99,11 @@ else if(UUID_FORMAT.equals(format)) {
10099
return new UUIDSchema();
101100
}
102101
else {
103-
return new StringSchema();
102+
if (StringUtils.isBlank(format)){
103+
return new StringSchema().format(null);
104+
}else {
105+
return new StringSchema().format(format);
106+
}
104107
}
105108
}
106109
else if(OBJECT_TYPE.equals(type)) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,6 @@ public void referringSpecWithoutComponentsTag() throws Exception {
657657
ParseOptions resolve = new ParseOptions();
658658
resolve.setResolveFully(true);
659659
final OpenAPI openAPI = new OpenAPIV3Parser().read("./ref-without-component/a.yaml", null, resolve);
660-
661660
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
662661
Assert.assertEquals("Example value", schemas.get("CustomerType").getExample());
663662
}

0 commit comments

Comments
 (0)