Skip to content

Commit 3e03257

Browse files
fix: same name and different location in param + small refactor
1 parent 676c821 commit 3e03257

File tree

4 files changed

+207
-157
lines changed

4 files changed

+207
-157
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
9696

9797
if(schema == null) {
9898
// stop! There's a problem. retain the original ref
99-
LOGGER.warn("unable to load model reference from `" + $ref + "`. It may not be available " +
100-
"or the reference isn't a valid model schema");
99+
LOGGER.warn("unable to load model reference from `{}`. It may not be available or the reference isn't a valid model schema", $ref);
101100
return $ref;
102101
}
103102
String newRef;
Lines changed: 93 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.swagger.v3.parser.processors;
22

3-
3+
import io.swagger.v3.oas.models.OpenAPI;
44
import io.swagger.v3.oas.models.examples.Example;
5-
import io.swagger.v3.oas.models.media.Content;
65
import io.swagger.v3.oas.models.media.MediaType;
76
import io.swagger.v3.oas.models.media.Schema;
87
import io.swagger.v3.oas.models.parameters.Parameter;
9-
import io.swagger.v3.oas.models.OpenAPI;
108
import io.swagger.v3.parser.ResolverCache;
119
import io.swagger.v3.parser.models.RefFormat;
1210

1311
import java.util.ArrayList;
1412
import java.util.List;
1513
import java.util.Map;
14+
import java.util.Optional;
1615

1716
import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
1817
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
@@ -33,137 +32,140 @@ public ParameterProcessor(ResolverCache cache, OpenAPI openAPI) {
3332
public ParameterProcessor(ResolverCache cache, OpenAPI openAPI, boolean openapi31) {
3433
this.cache = cache;
3534
this.openAPI = openAPI;
36-
this.schemaProcessor = new SchemaProcessor(cache,openAPI, openapi31);
37-
this.exampleProcessor = new ExampleProcessor(cache,openAPI);
35+
this.schemaProcessor = new SchemaProcessor(cache, openAPI, openapi31);
36+
this.exampleProcessor = new ExampleProcessor(cache, openAPI);
3837
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
3938
}
4039

4140
public void processParameter(Parameter parameter) {
42-
String $ref = parameter.get$ref();
43-
if($ref != null){
41+
processRefToExternalParameter(parameter, parameter.get$ref());
42+
if (parameter.getSchema() != null) {
43+
schemaProcessor.processSchema(parameter.getSchema());
44+
}
45+
processParameterExamples(parameter);
46+
processParameterMediaTypeSchema(parameter);
47+
}
48+
49+
private void processRefToExternalParameter(Parameter parameter, String referencePath) {
50+
if (referencePath != null) {
4451
RefFormat refFormat = computeRefFormat(parameter.get$ref());
45-
if (isAnExternalRefFormat(refFormat)){
46-
String newRef = externalRefProcessor.processRefToExternalParameter($ref, refFormat);
52+
if (isAnExternalRefFormat(refFormat)) {
53+
String newRef = externalRefProcessor.processRefToExternalParameter(referencePath, refFormat);
4754
if (newRef != null) {
4855
newRef = "#/components/parameters/" + newRef;
4956
parameter.set$ref(newRef);
5057
}
5158
}
5259
}
53-
if (parameter.getSchema() != null){
54-
schemaProcessor.processSchema(parameter.getSchema());
55-
}
56-
if (parameter.getExamples() != null){
57-
Map <String, Example> examples = parameter.getExamples();
58-
for(String exampleName: examples.keySet()){
59-
final Example example = examples.get(exampleName);
60-
exampleProcessor.processExample(example);
61-
}
60+
}
61+
62+
private void processParameterExamples(Parameter parameter) {
63+
Map<String, Example> examples = parameter.getExamples();
64+
if (examples != null) {
65+
examples.values().forEach(exampleProcessor::processExample);
6266
}
63-
Schema schema = null;
64-
if(parameter.getContent() != null) {
65-
Map<String,MediaType> content = parameter.getContent();
66-
for( String mediaName : content.keySet()) {
67+
}
68+
69+
private void processParameterMediaTypeSchema(Parameter parameter) {
70+
if (parameter.getContent() != null) {
71+
Map<String, MediaType> content = parameter.getContent();
72+
for (String mediaName : content.keySet()) {
6773
MediaType mediaType = content.get(mediaName);
68-
if(mediaType.getSchema()!= null) {
69-
schema = mediaType.getSchema();
70-
if (schema != null) {
71-
schemaProcessor.processSchema(schema);
72-
}
74+
if (mediaType.getSchema() != null) {
75+
Optional<Schema> schema = Optional.ofNullable(mediaType.getSchema());
76+
schema.ifPresent(schemaProcessor::processSchema);
7377
}
7478
}
7579
}
7680
}
7781

78-
public List<Parameter> processParameters(List<Parameter> parameters) {
82+
public List<Parameter> processParameters(List<Parameter> parameters) {
7983
if (parameters == null) {
8084
return null;
8185
}
82-
final List<Parameter> processedPathLevelParameters = new ArrayList<>();
8386
final List<Parameter> refParameters = new ArrayList<>();
8487

88+
final List<Parameter> processedPathLevelParameters = processPathLevelParams(parameters, refParameters);
89+
90+
for (Parameter resolvedParameter : refParameters) {
91+
int pos = 0;
92+
for (Parameter param : processedPathLevelParameters) {
93+
if (param.getName().equals(resolvedParameter.getName())) {
94+
// ref param wins
95+
processedPathLevelParameters.set(pos, resolvedParameter);
96+
break;
97+
}
98+
pos++;
99+
}
100+
101+
}
102+
processedPathLevelParameters.forEach(parameter -> {
103+
if (parameter.getSchema() != null) {
104+
schemaProcessor.processSchema(parameter.getSchema());
105+
} else if (parameter.getContent() != null) {
106+
Map<String, MediaType> content = parameter.getContent();
107+
content.values().forEach(mediaType -> {
108+
if (mediaType.getSchema() != null) {
109+
schemaProcessor.processSchema(mediaType.getSchema());
110+
}
111+
if (mediaType.getExamples() != null) {
112+
mediaType.getExamples().values().forEach(exampleProcessor::processExample);
113+
}
114+
});
115+
116+
}
117+
});
118+
119+
return processedPathLevelParameters;
120+
}
121+
122+
private List<Parameter> processPathLevelParams(List<Parameter> parameters, List<Parameter> refParameters) {
123+
final List<Parameter> processedPathLevelParameters = new ArrayList<>();
85124
for (Parameter parameter : parameters) {
86125
if (parameter.get$ref() != null) {
87126
RefFormat refFormat = computeRefFormat(parameter.get$ref());
88127
final Parameter resolvedParameter = cache.loadRef(parameter.get$ref(), refFormat, Parameter.class);
89-
if (parameter.get$ref().startsWith("#") && parameter.get$ref().indexOf("#/components/parameters") <= -1) {
128+
if (parameter.get$ref().startsWith("#") && !parameter.get$ref().contains("#/components/parameters")) {
90129
//TODO: Not possible to add warning during resolve doesn't accept result as an input. Hence commented below line.
91130
//result.warning(location, "The parameter should use Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.");
92131
continue;
93132
}
94-
if(resolvedParameter == null) {
133+
if (resolvedParameter == null) {
95134
// can't resolve it!
96135
processedPathLevelParameters.add(parameter);
97136
continue;
98137
}
99-
// if the parameter exists, replace it
100-
boolean matched = false;
101-
for(Parameter param : processedPathLevelParameters) {
102-
if(param != null && param.getName() != null && param.getName().equals(resolvedParameter.getName())) {
103-
// ref param wins
104-
matched = true;
105-
break;
106-
}
107-
}
108-
for(Parameter param : parameters) {
109-
if(param.getName() != null) {
110-
if (param.getName().equals(resolvedParameter.getName())) {
111-
// ref param wins
112-
matched = true;
113-
break;
114-
}
115-
}
116-
}
138+
boolean matched = isParameterExist(parameters, processedPathLevelParameters, resolvedParameter);
117139

118-
if(matched) {
140+
if (matched) {
119141
refParameters.add(resolvedParameter);
120-
}
121-
else {
142+
} else {
122143
processedPathLevelParameters.add(resolvedParameter);
123144
}
124-
}
125-
else {
145+
} else {
126146
processedPathLevelParameters.add(parameter);
127147
}
128148
}
149+
return processedPathLevelParameters;
150+
}
129151

130-
for(Parameter resolvedParameter : refParameters) {
131-
int pos = 0;
132-
for(Parameter param : processedPathLevelParameters) {
133-
if(param.getName().equals(resolvedParameter.getName())) {
134-
// ref param wins
135-
processedPathLevelParameters.set(pos, resolvedParameter);
136-
break;
137-
}
138-
pos++;
139-
}
140-
141-
}
142-
143-
for (Parameter parameter : processedPathLevelParameters) {
144-
Schema schema = parameter.getSchema();
145-
if(schema != null){
146-
schemaProcessor.processSchema(schema);
147-
}else if(parameter.getContent() != null){
148-
Map<String,MediaType> content = parameter.getContent();
149-
for( String mediaName : content.keySet()) {
150-
MediaType mediaType = content.get(mediaName);
151-
if(mediaType.getSchema()!= null) {
152-
schema = mediaType.getSchema();
153-
if (schema != null) {
154-
schemaProcessor.processSchema(schema);
155-
}
156-
}
157-
if(mediaType.getExamples() != null) {
158-
for(Example ex: mediaType.getExamples().values()){
159-
exampleProcessor.processExample(ex);
160-
}
161-
}
162-
}
163-
164-
}
152+
private static boolean isParameterExist(List<Parameter> parameters, List<Parameter> processedPathLevelParameters, Parameter resolvedParameter) {
153+
// verify if the parameter exists, if yes, then replace it when name and location are the same
154+
boolean matched = processedPathLevelParameters.stream()
155+
.anyMatch(param -> param != null
156+
&& param.getName() != null
157+
&& param.getIn() != null
158+
&& param.getName().equals(resolvedParameter.getName())
159+
&& param.getIn().equals(resolvedParameter.getIn()));
160+
161+
if (!matched) {
162+
matched = parameters.stream()
163+
.anyMatch(param -> param != null
164+
&& param.getName() != null
165+
&& param.getIn() != null
166+
&& param.getName().equals(resolvedParameter.getName())
167+
&& param.getIn().equals(resolvedParameter.getIn()));
165168
}
166-
167-
return processedPathLevelParameters;
169+
return matched;
168170
}
169171
}

0 commit comments

Comments
 (0)