diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java index 4217f72263..8f9c92c77e 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java @@ -252,22 +252,24 @@ private void processSchema(Schema property, String file) { } if (property instanceof ComposedSchema) { ComposedSchema composed = (ComposedSchema) property; - final Map refMap = Optional.ofNullable(property.getDiscriminator()) + final Map refMap = Optional.ofNullable(composed.getDiscriminator()) .map(Discriminator::getMapping).orElse(Collections.emptyMap()).entrySet() .stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); Map refCache = (!refMap.isEmpty() && - (property.getAnyOf() != null || property.getOneOf() != null)) ? - ((List)(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 -> { diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java index bd16ba580c..94cecd6754 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java @@ -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; @@ -265,15 +268,17 @@ else if(model instanceof ComposedSchema) { // Map to cache old - new refs in composed schemas Map refMappings = composedSchema.getDiscriminator() != null && composedSchema.getDiscriminator().getMapping() != null ? new HashMap<>() : null; - List 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 discriminatorMappings = composedSchema.getDiscriminator().getMapping(); diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java index c45ff8296d..9e6e7fafb2 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java @@ -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; @@ -158,21 +161,19 @@ public void processComposedSchema(ComposedSchema composedSchema) { } } } - if(composedSchema.getOneOf() != null || composedSchema.getAnyOf() != null){ - final List 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); + } + }); } }