Skip to content

Commit

Permalink
Consider anyOf & oneOf as mutually inclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
andurairaj committed May 19, 2024
1 parent ad69130 commit e7c9a46
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,24 @@ private void processSchema(Schema property, String file) {
}
if (property instanceof ComposedSchema) {
ComposedSchema composed = (ComposedSchema) property;
final Map<String, String> refMap = Optional.ofNullable(property.getDiscriminator())
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() &&
(property.getAnyOf() != null || property.getOneOf() != null)) ?
((List<Schema>)(property.getAnyOf() != null ? property.getAnyOf()
: property.getOneOf())).stream()
(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())) : 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() &&
(property.getAnyOf() != null || property.getOneOf() != null)) {
if (!refMap.isEmpty() && !refCache.isEmpty()) {
refCache.entrySet()
.stream().filter(e -> !e.getKey().equals(e.getValue().get$ref()))
.forEach(entry -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +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 @@ -265,15 +268,17 @@ else if(model instanceof ComposedSchema) {
// Map to cache old - new refs in composed schemas
Map<String, String> refMappings = composedSchema.getDiscriminator() != null &&
composedSchema.getDiscriminator().getMapping() != null ? new HashMap<>() : null;
List<Schema> schemas = composedSchema.getAnyOf() != null ? composedSchema.getAnyOf()
: composedSchema.getOneOf();
for (Schema innerModel : schemas) {
String oldRef = innerModel.get$ref();
updateRefs(innerModel, pathRef);
if(oldRef != null && refMappings != null && !oldRef.equals(innerModel.get$ref())) {
refMappings.put(oldRef, innerModel.get$ref());
}
}

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();
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 @@ -158,21 +161,19 @@ public void processComposedSchema(ComposedSchema composedSchema) {
}
}
}
if(composedSchema.getOneOf() != null || composedSchema.getAnyOf() != null){
final List<Schema> schemas = composedSchema.getOneOf() != null ?
composedSchema.getOneOf() : composedSchema.getAnyOf();
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.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);
}
});
}
}

Expand Down

0 comments on commit e7c9a46

Please sign in to comment.