Skip to content

Commit 0c2cec6

Browse files
andurairajfrantuma
authored andcommitted
Fix ref handling in Discriminator mappings
1 parent 0493186 commit 0c2cec6

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
import java.net.URI;
55
import java.nio.file.Paths;
66
import java.util.Collection;
7+
import java.util.Collections;
78
import java.util.LinkedHashMap;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Objects;
12+
import java.util.Optional;
13+
import java.util.function.Function;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
1116

1217
import io.swagger.v3.oas.models.Components;
1318
import io.swagger.v3.oas.models.OpenAPI;
@@ -246,9 +251,31 @@ private void processSchema(Schema property, String file) {
246251
}
247252
if (property instanceof ComposedSchema) {
248253
ComposedSchema composed = (ComposedSchema) property;
254+
final Map<String, String> refMap = Optional.ofNullable(composed.getDiscriminator())
255+
.map(Discriminator::getMapping).orElse(Collections.emptyMap()).entrySet()
256+
.stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
257+
Map<String, Schema> refCache = (!refMap.isEmpty() &&
258+
(composed.getAnyOf() != null || composed.getOneOf() != null)) ? Stream.of(
259+
composed.getAnyOf(), composed.getOneOf()
260+
)
261+
.filter(Objects::nonNull)
262+
.filter(l -> !l.isEmpty())
263+
.flatMap(Collection::stream)
264+
.filter(s -> s.get$ref() != null)
265+
.collect(Collectors.toMap(Schema::get$ref, Function.identity())) : Collections.emptyMap();
266+
249267
processProperties(composed.getAllOf(), file);
250268
processProperties(composed.getAnyOf(), file);
251269
processProperties(composed.getOneOf(), file);
270+
271+
if (!refMap.isEmpty() && !refCache.isEmpty()) {
272+
refCache.entrySet()
273+
.stream().filter(e -> !e.getKey().equals(e.getValue().get$ref()))
274+
.forEach(entry -> {
275+
String newRef = entry.getValue().get$ref();
276+
property.getDiscriminator().getMapping().put(refMap.get(entry.getKey()), newRef);
277+
});
278+
}
252279
}
253280
}
254281
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,33 @@ else if(model instanceof ComposedSchema) {
256256
for (Schema innerModel : composedSchema.getAllOf()) {
257257
updateRefs(innerModel, pathRef);
258258
}
259-
}if (composedSchema.getAnyOf() != null) {
260-
for(Schema innerModel : composedSchema.getAnyOf()) {
261-
updateRefs(innerModel, pathRef);
262-
}
263-
}if (composedSchema.getOneOf() != null) {
264-
for (Schema innerModel : composedSchema.getOneOf()) {
265-
updateRefs(innerModel, pathRef);
259+
}
260+
if (composedSchema.getAnyOf() != null || composedSchema.getOneOf() != null) {
261+
// Map to cache old - new refs in composed schemas
262+
Map<String, String> refMappings = composedSchema.getDiscriminator() != null &&
263+
composedSchema.getDiscriminator().getMapping() != null ? new HashMap<>() : null;
264+
265+
Stream.of(composedSchema.getAnyOf(), composedSchema.getOneOf())
266+
.filter(Objects::nonNull).filter(l -> !l.isEmpty())
267+
.flatMap(Collection::stream)
268+
.forEach(innerModel -> {
269+
String oldRef = innerModel.get$ref();
270+
updateRefs(innerModel, pathRef);
271+
if(oldRef != null && refMappings != null && !oldRef.equals(innerModel.get$ref())) {
272+
refMappings.put(oldRef, innerModel.get$ref());
273+
}
274+
});
275+
// Update refs in discriminator mappings
276+
if(refMappings != null && !refMappings.isEmpty()) {
277+
Map<String, String> discriminatorMappings = composedSchema.getDiscriminator().getMapping();
278+
for(String key : discriminatorMappings.keySet()) {
279+
if(refMappings.containsKey(discriminatorMappings.get(key))) {
280+
discriminatorMappings.put(key, refMappings.get(discriminatorMappings.get(key)));
281+
}
282+
}
266283
}
267284
}
285+
268286
}
269287
else if(model instanceof ArraySchema) {
270288
ArraySchema arraySchema = (ArraySchema) model;

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
import io.swagger.v3.parser.util.OpenAPIDeserializer;
1515
import io.swagger.v3.parser.util.RefUtils;
1616

17+
import java.util.Collection;
1718
import java.util.HashMap;
1819
import java.util.List;
1920
import java.util.Map;
21+
import java.util.Objects;
22+
import java.util.stream.Stream;
2023

2124
import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
2225
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
@@ -157,33 +160,21 @@ public void processComposedSchema(ComposedSchema composedSchema) {
157160
}
158161
}
159162
}
160-
}if(composedSchema.getOneOf() != null){
161-
final List<Schema> schemas = composedSchema.getOneOf();
162-
if (schemas != null) {
163-
for (Schema schema : schemas) {
164-
if (schema.get$ref() != null) {
165-
String oldRef = schema.get$ref();
166-
processReferenceSchema(schema);
167-
String newRef = schema.get$ref();
168-
changeDiscriminatorMapping(composedSchema, oldRef, newRef);
169-
} else {
170-
processSchemaType(schema);
171-
}
172-
}
173-
}
174-
}if(composedSchema.getAnyOf() != null){
175-
final List<Schema> schemas = composedSchema.getAnyOf();
176-
if (schemas != null) {
177-
for (Schema schema : schemas) {
178-
if (schema.get$ref() != null) {
179-
processReferenceSchema(schema);
180-
} else {
181-
processSchemaType(schema);
182-
}
183-
}
184-
}
185163
}
186-
164+
if(composedSchema.getOneOf() != null || composedSchema.getAnyOf() != null) {
165+
Stream.of(composedSchema.getOneOf(), composedSchema.getAnyOf())
166+
.filter(Objects::nonNull).filter(l -> !l.isEmpty()).flatMap(Collection::stream)
167+
.forEach(schema -> {
168+
if (schema.get$ref() != null) {
169+
String oldRef = schema.get$ref();
170+
processReferenceSchema(schema);
171+
String newRef = schema.get$ref();
172+
changeDiscriminatorMapping(composedSchema, oldRef, newRef);
173+
} else {
174+
processSchemaType(schema);
175+
}
176+
});
177+
}
187178
}
188179

189180
private void changeDiscriminatorMapping(ComposedSchema composedSchema, String oldRef, String newRef) {

0 commit comments

Comments
 (0)