Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {

if(schema == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn("unable to load model reference from `" + $ref + "`. It may not be available " +
"or the reference isn't a valid model schema");
LOGGER.warn("unable to load model reference from `{}`. It may not be available or the reference isn't a valid model schema", $ref);
return $ref;
}
String newRef;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.swagger.v3.parser.processors;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.ResolverCache;
import io.swagger.v3.parser.models.RefFormat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
Expand All @@ -33,137 +32,140 @@ public ParameterProcessor(ResolverCache cache, OpenAPI openAPI) {
public ParameterProcessor(ResolverCache cache, OpenAPI openAPI, boolean openapi31) {
this.cache = cache;
this.openAPI = openAPI;
this.schemaProcessor = new SchemaProcessor(cache,openAPI, openapi31);
this.exampleProcessor = new ExampleProcessor(cache,openAPI);
this.schemaProcessor = new SchemaProcessor(cache, openAPI, openapi31);
this.exampleProcessor = new ExampleProcessor(cache, openAPI);
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
}

public void processParameter(Parameter parameter) {
String $ref = parameter.get$ref();
if($ref != null){
processRefToExternalParameter(parameter, parameter.get$ref());
if (parameter.getSchema() != null) {
schemaProcessor.processSchema(parameter.getSchema());
}
processParameterExamples(parameter);
processParameterMediaTypeSchema(parameter);
}

private void processRefToExternalParameter(Parameter parameter, String referencePath) {
if (referencePath != null) {
RefFormat refFormat = computeRefFormat(parameter.get$ref());
if (isAnExternalRefFormat(refFormat)){
String newRef = externalRefProcessor.processRefToExternalParameter($ref, refFormat);
if (isAnExternalRefFormat(refFormat)) {
String newRef = externalRefProcessor.processRefToExternalParameter(referencePath, refFormat);
if (newRef != null) {
newRef = "#/components/parameters/" + newRef;
parameter.set$ref(newRef);
}
}
}
if (parameter.getSchema() != null){
schemaProcessor.processSchema(parameter.getSchema());
}
if (parameter.getExamples() != null){
Map <String, Example> examples = parameter.getExamples();
for(String exampleName: examples.keySet()){
final Example example = examples.get(exampleName);
exampleProcessor.processExample(example);
}
}

private void processParameterExamples(Parameter parameter) {
Map<String, Example> examples = parameter.getExamples();
if (examples != null) {
examples.values().forEach(exampleProcessor::processExample);
}
Schema schema = null;
if(parameter.getContent() != null) {
Map<String,MediaType> content = parameter.getContent();
for( String mediaName : content.keySet()) {
}

private void processParameterMediaTypeSchema(Parameter parameter) {
if (parameter.getContent() != null) {
Map<String, MediaType> content = parameter.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if(mediaType.getSchema()!= null) {
schema = mediaType.getSchema();
if (schema != null) {
schemaProcessor.processSchema(schema);
}
if (mediaType.getSchema() != null) {
Optional<Schema> schema = Optional.ofNullable(mediaType.getSchema());
schema.ifPresent(schemaProcessor::processSchema);
}
}
}
}

public List<Parameter> processParameters(List<Parameter> parameters) {
public List<Parameter> processParameters(List<Parameter> parameters) {
if (parameters == null) {
return null;
}
final List<Parameter> processedPathLevelParameters = new ArrayList<>();
final List<Parameter> refParameters = new ArrayList<>();

final List<Parameter> processedPathLevelParameters = processPathLevelParams(parameters, refParameters);

for (Parameter resolvedParameter : refParameters) {
int pos = 0;
for (Parameter param : processedPathLevelParameters) {
if (param.getName().equals(resolvedParameter.getName())) {
// ref param wins
processedPathLevelParameters.set(pos, resolvedParameter);
break;
}
pos++;
}

}
processedPathLevelParameters.forEach(parameter -> {
if (parameter.getSchema() != null) {
schemaProcessor.processSchema(parameter.getSchema());
} else if (parameter.getContent() != null) {
Map<String, MediaType> content = parameter.getContent();
content.values().forEach(mediaType -> {
if (mediaType.getSchema() != null) {
schemaProcessor.processSchema(mediaType.getSchema());
}
if (mediaType.getExamples() != null) {
mediaType.getExamples().values().forEach(exampleProcessor::processExample);
}
});

}
});

return processedPathLevelParameters;
}

private List<Parameter> processPathLevelParams(List<Parameter> parameters, List<Parameter> refParameters) {
final List<Parameter> processedPathLevelParameters = new ArrayList<>();
for (Parameter parameter : parameters) {
if (parameter.get$ref() != null) {
RefFormat refFormat = computeRefFormat(parameter.get$ref());
final Parameter resolvedParameter = cache.loadRef(parameter.get$ref(), refFormat, Parameter.class);
if (parameter.get$ref().startsWith("#") && parameter.get$ref().indexOf("#/components/parameters") <= -1) {
if (parameter.get$ref().startsWith("#") && !parameter.get$ref().contains("#/components/parameters")) {
//TODO: Not possible to add warning during resolve doesn't accept result as an input. Hence commented below line.
//result.warning(location, "The parameter should use Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.");
continue;
}
if(resolvedParameter == null) {
if (resolvedParameter == null) {
// can't resolve it!
processedPathLevelParameters.add(parameter);
continue;
}
// if the parameter exists, replace it
boolean matched = false;
for(Parameter param : processedPathLevelParameters) {
if(param != null && param.getName() != null && param.getName().equals(resolvedParameter.getName())) {
// ref param wins
matched = true;
break;
}
}
for(Parameter param : parameters) {
if(param.getName() != null) {
if (param.getName().equals(resolvedParameter.getName())) {
// ref param wins
matched = true;
break;
}
}
}
boolean matched = isParameterExist(parameters, processedPathLevelParameters, resolvedParameter);

if(matched) {
if (matched) {
refParameters.add(resolvedParameter);
}
else {
} else {
processedPathLevelParameters.add(resolvedParameter);
}
}
else {
} else {
processedPathLevelParameters.add(parameter);
}
}
return processedPathLevelParameters;
}

for(Parameter resolvedParameter : refParameters) {
int pos = 0;
for(Parameter param : processedPathLevelParameters) {
if(param.getName().equals(resolvedParameter.getName())) {
// ref param wins
processedPathLevelParameters.set(pos, resolvedParameter);
break;
}
pos++;
}

}

for (Parameter parameter : processedPathLevelParameters) {
Schema schema = parameter.getSchema();
if(schema != null){
schemaProcessor.processSchema(schema);
}else if(parameter.getContent() != null){
Map<String,MediaType> content = parameter.getContent();
for( String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if(mediaType.getSchema()!= null) {
schema = mediaType.getSchema();
if (schema != null) {
schemaProcessor.processSchema(schema);
}
}
if(mediaType.getExamples() != null) {
for(Example ex: mediaType.getExamples().values()){
exampleProcessor.processExample(ex);
}
}
}

}
private static boolean isParameterExist(List<Parameter> parameters, List<Parameter> processedPathLevelParameters, Parameter resolvedParameter) {
// verify if the parameter exists, if yes, then replace it when name and location are the same
boolean matched = processedPathLevelParameters.stream()
.anyMatch(param -> param != null
&& param.getName() != null
&& param.getIn() != null
&& param.getName().equals(resolvedParameter.getName())
&& param.getIn().equals(resolvedParameter.getIn()));

if (!matched) {
matched = parameters.stream()
.anyMatch(param -> param != null
&& param.getName() != null
&& param.getIn() != null
&& param.getName().equals(resolvedParameter.getName())
&& param.getIn().equals(resolvedParameter.getIn()));
}

return processedPathLevelParameters;
return matched;
}
}
Loading
Loading