|
31 | 31 | import java.util.Arrays;
|
32 | 32 | import java.util.Collections;
|
33 | 33 | import java.util.LinkedHashMap;
|
| 34 | +import java.util.LinkedHashSet; |
34 | 35 | import java.util.List;
|
35 | 36 | import java.util.Map;
|
36 | 37 | import java.util.Optional;
|
| 38 | +import java.util.Set; |
37 | 39 |
|
38 | 40 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
39 | 41 | import com.fasterxml.jackson.annotation.JsonView;
|
|
65 | 67 |
|
66 | 68 | /**
|
67 | 69 | * The type Spring doc annotations utils.
|
| 70 | + * |
68 | 71 | * @author bnasslahsen
|
69 | 72 | */
|
70 | 73 | @SuppressWarnings({ "rawtypes" })
|
@@ -142,10 +145,21 @@ public static Schema extractSchema(Components components, Type returnType, JsonV
|
142 | 145 | for (Map.Entry<String, Schema> entry : schemaMap.entrySet()) {
|
143 | 146 | // If we've seen this schema before but find later it should be polymorphic,
|
144 | 147 | // replace the existing schema with this richer version.
|
| 148 | + Schema existingSchema = componentSchemas.get(entry.getKey()); |
145 | 149 | if (!componentSchemas.containsKey(entry.getKey()) ||
|
146 |
| - (!entry.getValue().getClass().equals(componentSchemas.get(entry.getKey()).getClass()) && entry.getValue().getAllOf() != null)) { |
| 150 | + (!entry.getValue().getClass().equals(existingSchema.getClass()) && entry.getValue().getAllOf() != null)) { |
147 | 151 | componentSchemas.put(entry.getKey(), entry.getValue());
|
148 | 152 | }
|
| 153 | + else if (componentSchemas.containsKey(entry.getKey()) && schemaMap.containsKey(entry.getKey())) { |
| 154 | + // Check to merge polymorphic types |
| 155 | + Set<Schema> existingAllOf = new LinkedHashSet<>(); |
| 156 | + if(existingSchema.getAllOf() != null) |
| 157 | + existingAllOf.addAll(existingSchema.getAllOf()); |
| 158 | + if (schemaMap.get(entry.getKey()).getAllOf() != null){ |
| 159 | + existingAllOf.addAll(schemaMap.get(entry.getKey()).getAllOf()); |
| 160 | + existingSchema.setAllOf(new ArrayList<>(existingAllOf)); |
| 161 | + } |
| 162 | + } |
149 | 163 | }
|
150 | 164 | components.setSchemas(componentSchemas);
|
151 | 165 | }
|
@@ -207,8 +221,8 @@ public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.C
|
207 | 221 | * Merge schema.
|
208 | 222 | *
|
209 | 223 | * @param existingContent the existing content
|
210 |
| - * @param schemaN the schema n |
211 |
| - * @param mediaTypeStr the media type str |
| 224 | + * @param schemaN the schema n |
| 225 | + * @param mediaTypeStr the media type str |
212 | 226 | */
|
213 | 227 | public static void mergeSchema(Content existingContent, Schema<?> schemaN, String mediaTypeStr) {
|
214 | 228 | if (existingContent.containsKey(mediaTypeStr)) {
|
@@ -322,7 +336,7 @@ private static void addExtension(io.swagger.v3.oas.annotations.media.Content ann
|
322 | 336 | * Sets examples.
|
323 | 337 | *
|
324 | 338 | * @param mediaType the media type
|
325 |
| - * @param examples the examples |
| 339 | + * @param examples the examples |
326 | 340 | */
|
327 | 341 | private static void setExamples(MediaType mediaType, ExampleObject[] examples) {
|
328 | 342 | if (examples.length == 1 && StringUtils.isBlank(examples[0].name())) {
|
@@ -436,7 +450,7 @@ private static boolean isArray(io.swagger.v3.oas.annotations.media.Content annot
|
436 | 450 | * Resolve default value object.
|
437 | 451 | *
|
438 | 452 | * @param defaultValueStr the default value str
|
439 |
| - * @param objectMapper the object mapper |
| 453 | + * @param objectMapper the object mapper |
440 | 454 | * @return the object
|
441 | 455 | */
|
442 | 456 | public static Object resolveDefaultValue(String defaultValueStr, ObjectMapper objectMapper) {
|
|
0 commit comments