|
32 | 32 | import java.util.ArrayList;
|
33 | 33 | import java.util.Arrays;
|
34 | 34 | import java.util.Collections;
|
| 35 | +import java.util.HashMap; |
35 | 36 | import java.util.LinkedHashMap;
|
36 | 37 | import java.util.LinkedHashSet;
|
37 | 38 | import java.util.List;
|
|
44 | 45 | import com.fasterxml.jackson.annotation.JsonView;
|
45 | 46 | import com.fasterxml.jackson.databind.ObjectMapper;
|
46 | 47 | import io.swagger.v3.core.converter.AnnotatedType;
|
| 48 | +import io.swagger.v3.core.converter.ModelConverterContext; |
| 49 | +import io.swagger.v3.core.converter.ModelConverterContextImpl; |
47 | 50 | import io.swagger.v3.core.converter.ModelConverters;
|
48 | 51 | import io.swagger.v3.core.converter.ResolvedSchema;
|
49 | 52 | import io.swagger.v3.core.util.AnnotationsUtils;
|
|
63 | 66 | import io.swagger.v3.oas.models.media.StringSchema;
|
64 | 67 | import org.apache.commons.lang3.ArrayUtils;
|
65 | 68 | import org.apache.commons.lang3.StringUtils;
|
| 69 | +import org.jetbrains.annotations.Nullable; |
66 | 70 | import org.slf4j.Logger;
|
67 | 71 | import org.slf4j.LoggerFactory;
|
68 | 72 |
|
@@ -91,6 +95,11 @@ public class SpringDocAnnotationsUtils extends AnnotationsUtils {
|
91 | 95 | */
|
92 | 96 | private static final List<Class> ANNOTATIONS_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
|
93 | 97 |
|
| 98 | + /** |
| 99 | + * The reusable context |
| 100 | + */ |
| 101 | + public static final Map<SpecVersion, ModelConverterContext> CONTEXT = new HashMap<>(); |
| 102 | + |
94 | 103 | static {
|
95 | 104 | ANNOTATIONS_TO_IGNORE.add(Hidden.class);
|
96 | 105 | ANNOTATIONS_TO_IGNORE.add(JsonIgnore.class);
|
@@ -131,18 +140,15 @@ public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Compon
|
131 | 140 | public static Schema extractSchema(Components components, Type returnType, JsonView jsonView, Annotation[] annotations, SpecVersion specVersion) {
|
132 | 141 | if (returnType == null) return null;
|
133 | 142 | Schema schemaN = null;
|
134 |
| - ResolvedSchema resolvedSchema; |
135 | 143 | boolean openapi31 = SpecVersion.V31 == specVersion;
|
136 |
| - try { |
137 |
| - resolvedSchema = ModelConverters.getInstance(openapi31) |
138 |
| - .resolveAsResolvedSchema( |
139 |
| - new AnnotatedType(returnType) |
140 |
| - .resolveAsRef(true).jsonViewAnnotation(jsonView).ctxAnnotations(annotations)); |
141 |
| - } |
142 |
| - catch (Exception e) { |
143 |
| - LOGGER.warn(Constants.GRACEFUL_EXCEPTION_OCCURRED, e); |
144 |
| - return null; |
| 144 | + if (jsonView != null) { |
| 145 | + annotations = ArrayUtils.addAll(annotations, jsonView); |
145 | 146 | }
|
| 147 | + var aType = new AnnotatedType(returnType) |
| 148 | + .resolveAsRef(true) |
| 149 | + .jsonViewAnnotation(jsonView) |
| 150 | + .ctxAnnotations(annotations); |
| 151 | + ResolvedSchema resolvedSchema = resolveAsResolvedSchema(specVersion, aType); |
146 | 152 | if (resolvedSchema != null) {
|
147 | 153 | Map<String, Schema> schemaMap = resolvedSchema.referencedSchemas;
|
148 | 154 | if (!CollectionUtils.isEmpty(schemaMap) && components != null) {
|
@@ -187,6 +193,25 @@ else if (componentSchemas.containsKey(entry.getKey()) && schemaMap.containsKey(e
|
187 | 193 | return schemaN;
|
188 | 194 | }
|
189 | 195 |
|
| 196 | + @Nullable |
| 197 | + private static ResolvedSchema resolveAsResolvedSchema(SpecVersion specVersion, AnnotatedType type) { |
| 198 | + ResolvedSchema resolvedSchema; |
| 199 | + try { |
| 200 | + var context = CONTEXT.computeIfAbsent(specVersion, k -> { |
| 201 | + boolean openapi31 = SpecVersion.V31 == specVersion; |
| 202 | + return new ModelConverterContextImpl(ModelConverters.getInstance(openapi31).getConverters()); |
| 203 | + }); |
| 204 | + resolvedSchema = new ResolvedSchema(); |
| 205 | + resolvedSchema.schema = context.resolve(type); |
| 206 | + resolvedSchema.referencedSchemas = context.getDefinedModels(); |
| 207 | + } |
| 208 | + catch (Exception e) { |
| 209 | + LOGGER.warn(Constants.GRACEFUL_EXCEPTION_OCCURRED, e); |
| 210 | + return null; |
| 211 | + } |
| 212 | + return resolvedSchema; |
| 213 | + } |
| 214 | + |
190 | 215 | /**
|
191 | 216 | * Gets content.
|
192 | 217 | *
|
|
0 commit comments