Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ref handling in CB, AnyOf & examples (fixes : #2086) #2087

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 @@ -4,10 +4,15 @@
import java.net.URI;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
Expand All @@ -29,6 +34,7 @@
import io.swagger.v3.parser.ResolverCache;
import io.swagger.v3.parser.models.RefFormat;
import io.swagger.v3.parser.models.RefType;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -246,9 +252,31 @@ private void processSchema(Schema property, String file) {
}
if (property instanceof ComposedSchema) {
ComposedSchema composed = (ComposedSchema) property;
final Map<String, String> refMap = Optional.ofNullable(composed.getDiscriminator())
.map(Discriminator::getMapping).orElse(Collections.emptyMap()).entrySet()
.stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Map<String, Schema> refCache = (!refMap.isEmpty() &&
(composed.getAnyOf() != null || composed.getOneOf() != null)) ? Stream.of(
composed.getAnyOf(), composed.getOneOf()
)
.filter(Objects::nonNull)
.filter(l -> !l.isEmpty())
.flatMap(Collection::stream)
.filter(s -> s.get$ref() != null)
.collect(Collectors.toMap(Schema::get$ref, Function.identity())) : Collections.emptyMap();

processProperties(composed.getAllOf(), file);
processProperties(composed.getAnyOf(), file);
processProperties(composed.getOneOf(), file);

if (!refMap.isEmpty() && !refCache.isEmpty()) {
refCache.entrySet()
.stream().filter(e -> !e.getKey().equals(e.getValue().get$ref()))
.forEach(entry -> {
String newRef = entry.getValue().get$ref();
property.getDiscriminator().getMapping().put(refMap.get(entry.getKey()), newRef);
});
}
}
}
}
Expand Down Expand Up @@ -298,71 +326,75 @@ public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) {
cache.putRenamedRef($ref, newRef);

if(pathItem != null) {
if(pathItem.readOperationsMap() != null) {
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
Schema schema = null;
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
if (mediaType.getExamples() != null) {
processRefExamples(mediaType.getExamples(), $ref);
}
processPathItem(pathItem, $ref);
}

return pathItem;
}

private void processPathItem(PathItem pathItem, String $ref) {
if(pathItem.readOperationsMap() != null) {
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
Schema schema = null;
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
if (mediaType.getExamples() != null) {
processRefExamples(mediaType.getExamples(), $ref);
}

}
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
if (response.getHeaders() != null) {
processRefHeaders(response.getHeaders(), $ref);
}
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
if (response.getHeaders() != null) {
processRefHeaders(response.getHeaders(), $ref);
}
}
}
}
if (operation.getRequestBody() != null) {
RequestBody body = operation.getRequestBody();
if (body.getContent() != null) {
Schema schema;
Map<String, MediaType> content = body.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
}
if (operation.getRequestBody() != null) {
RequestBody body = operation.getRequestBody();
if (body.getContent() != null) {
Schema schema;
Map<String, MediaType> content = body.getContent();
for (String mediaName : content.keySet()) {
MediaType mediaType = content.get(mediaName);
if (mediaType.getSchema() != null) {
schema = mediaType.getSchema();
if (schema != null) {
processRefSchemaObject(mediaType.getSchema(), $ref);
}
}
}
}
}

final List<Parameter> parameters = operation.getParameters();
if (parameters != null) {
parameters.stream()
.filter(parameter -> parameter.getSchema() != null)
.forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref));
}
final List<Parameter> parameters = operation.getParameters();
if (parameters != null) {
parameters.stream()
.filter(parameter -> parameter.getSchema() != null)
.forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref));
}
}
}

return pathItem;
}

private void processDiscriminator(Discriminator d, String file) {
Expand Down Expand Up @@ -838,6 +870,9 @@ public String processRefToExternalParameter(String $ref, RefFormat refFormat) {
if(parameter.getSchema() != null){
processRefSchemaObject(parameter.getSchema(), $ref);
}
if(parameter.getExamples() != null) {
processRefExamples(parameter.getExamples(), $ref);
}
}

return newRef;
Expand Down Expand Up @@ -897,6 +932,24 @@ public String processRefToExternalCallback(String $ref, RefFormat refFormat) {
processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE);
}
}
} else {
for (String path : callback.keySet()) {
PathItem pathItem = callback.get(path);
if(pathItem != null) {
if (pathItem.get$ref() != null) {
RefFormat pathRefFormat = computeRefFormat(pathItem.get$ref());
String path$ref = pathItem.get$ref();
if (isAnExternalRefFormat(refFormat)) {
pathItem = this.processRefToExternalPathItem(path$ref, pathRefFormat);
} else {
pathItem = cache.loadRef(pathItem.get$ref(), refFormat, PathItem.class);
}
callback.put(path, pathItem);
} else {
this.processPathItem(pathItem, $ref);
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import io.swagger.v3.parser.models.RefFormat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
Expand Down Expand Up @@ -155,6 +159,9 @@ protected void updateRefs(PathItem path, String pathRef) {
for (String name : callbacks.keySet()) {
Callback callback = callbacks.get(name);
if (callback != null) {
if(callback.get$ref() != null) {
callback.set$ref(computeRef(callback.get$ref(), pathRef));
}
for(String callbackName : callback.keySet()) {
PathItem pathItem = callback.get(callbackName);
updateRefs(pathItem,pathRef);
Expand Down Expand Up @@ -256,15 +263,33 @@ else if(model instanceof ComposedSchema) {
for (Schema innerModel : composedSchema.getAllOf()) {
updateRefs(innerModel, pathRef);
}
}if (composedSchema.getAnyOf() != null) {
for(Schema innerModel : composedSchema.getAnyOf()) {
updateRefs(innerModel, pathRef);
}
}if (composedSchema.getOneOf() != null) {
for (Schema innerModel : composedSchema.getOneOf()) {
updateRefs(innerModel, pathRef);
}
if (composedSchema.getAnyOf() != null || composedSchema.getOneOf() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AS mentioned above:

IIANM the changes here consider oneOf and anyOf as mutually exclusive, while they can coexist even if not so common. I would suggest to consider them both in the processing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the processing to be mutually inclusive.
Intuitively though, the definitions feel that they are mutually exclusive.

// Map to cache old - new refs in composed schemas
Map<String, String> refMappings = composedSchema.getDiscriminator() != null &&
composedSchema.getDiscriminator().getMapping() != null ? new HashMap<>() : null;

Stream.of(composedSchema.getAnyOf(), composedSchema.getOneOf())
.filter(Objects::nonNull).filter(l -> !l.isEmpty())
.flatMap(Collection::stream)
.forEach(innerModel -> {
String oldRef = innerModel.get$ref();
updateRefs(innerModel, pathRef);
if(oldRef != null && refMappings != null && !oldRef.equals(innerModel.get$ref())) {
refMappings.put(oldRef, innerModel.get$ref());
}
});
// Update refs in discriminator mappings
if(refMappings != null && !refMappings.isEmpty()) {
Map<String, String> discriminatorMappings = composedSchema.getDiscriminator().getMapping();
for(String key : discriminatorMappings.keySet()) {
if(refMappings.containsKey(discriminatorMappings.get(key))) {
discriminatorMappings.put(key, refMappings.get(discriminatorMappings.get(key)));
}
}
}
}

}
else if(model instanceof ArraySchema) {
ArraySchema arraySchema = (ArraySchema) model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import io.swagger.v3.parser.util.OpenAPIDeserializer;
import io.swagger.v3.parser.util.RefUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
Expand Down Expand Up @@ -157,33 +160,21 @@ public void processComposedSchema(ComposedSchema composedSchema) {
}
}
}
}if(composedSchema.getOneOf() != null){
final List<Schema> schemas = composedSchema.getOneOf();
if (schemas != null) {
for (Schema schema : schemas) {
if (schema.get$ref() != null) {
String oldRef = schema.get$ref();
processReferenceSchema(schema);
String newRef = schema.get$ref();
changeDiscriminatorMapping(composedSchema, oldRef, newRef);
} else {
processSchemaType(schema);
}
}
}
}if(composedSchema.getAnyOf() != null){
final List<Schema> schemas = composedSchema.getAnyOf();
if (schemas != null) {
for (Schema schema : schemas) {
if (schema.get$ref() != null) {
processReferenceSchema(schema);
} else {
processSchemaType(schema);
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AS mentioned above:

IIANM the changes here consider oneOf and anyOf as mutually exclusive, while they can coexist even if not so common. I would suggest to consider them both in the processing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as suggested.


if(composedSchema.getOneOf() != null || composedSchema.getAnyOf() != null) {
Stream.of(composedSchema.getOneOf(), composedSchema.getAnyOf())
.filter(Objects::nonNull).filter(l -> !l.isEmpty()).flatMap(Collection::stream)
.forEach(schema -> {
if (schema.get$ref() != null) {
String oldRef = schema.get$ref();
processReferenceSchema(schema);
String newRef = schema.get$ref();
changeDiscriminatorMapping(composedSchema, oldRef, newRef);
} else {
processSchemaType(schema);
}
});
}
}

private void changeDiscriminatorMapping(ComposedSchema composedSchema, String oldRef, String newRef) {
Expand Down
Loading