1 package io.swagger.v3.core.jackson;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import com.fasterxml.jackson.core.Version;
5 import com.fasterxml.jackson.databind.AnnotationIntrospector;
6 import com.fasterxml.jackson.databind.introspect.Annotated;
7 import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
8 import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
9 import com.fasterxml.jackson.databind.jsontype.NamedType;
10 import io.swagger.v3.core.util.AnnotationsUtils;
11 import io.swagger.v3.oas.annotations.Hidden;
12 import io.swagger.v3.oas.annotations.media.ArraySchema;
13 import io.swagger.v3.oas.annotations.media.Schema;
14
15 import javax.xml.bind.annotation.XmlElement;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19
20 public class SwaggerAnnotationIntrospector extends AnnotationIntrospector {
21 private static final long serialVersionUID = 1L;
22
23 @Override
24 public Version version() {
25 return PackageVersion.VERSION;
26 }
27
28 @Override
29 public boolean hasIgnoreMarker(AnnotatedMember m) {
30 Schema ann = m.getAnnotation(Schema.class);
31 if (ann != null && ann.hidden()) {
32 return true;
33 }
34 Hidden hidden = m.getAnnotation(Hidden.class);
35 if (hidden != null) {
36 return true;
37 }
38 return false;
39 }
40
41 @Override
42 public Boolean hasRequiredMarker(AnnotatedMember m) {
43 XmlElement elem = m.getAnnotation(XmlElement.class);
44 if (elem != null) {
45 if (elem.required()) {
46 return true;
47 }
48 }
49 JsonProperty jsonProperty = m.getAnnotation(JsonProperty.class);
50 if (jsonProperty != null) {
51 if (jsonProperty.required()) {
52 return true;
53 }
54 }
55 Schema ann = m.getAnnotation(Schema.class);
56 if (ann != null) {
57 if (ann.required()) {
58 return ann.required();
59 }
60 }
61 ArraySchema arraySchema = m.getAnnotation(ArraySchema.class);
62 if (arraySchema != null) {
63 if (arraySchema.arraySchema().required()) {
64 return arraySchema.arraySchema().required();
65 }
66 if (arraySchema.schema().required()) {
67 return arraySchema.schema().required();
68 }
69 }
70 return null;
71 }
72
73 @Override
74 public String findPropertyDescription(Annotated a) {
75 Schema model = a.getAnnotation(Schema.class);
76 if (model != null && !"".equals(model.description())) {
77 return model.description();
78 }
79
80 return null;
81 }
82
83 @Override
84 public List<NamedType> findSubtypes(Annotated a) {
85 Schema schema = a.getAnnotation(Schema.class);
86 if (schema == null) {
87 final ArraySchema arraySchema = a.getAnnotation(ArraySchema.class);
88 if (arraySchema != null) {
89 schema = arraySchema.schema();
90 }
91 }
92
93 if (AnnotationsUtils.hasSchemaAnnotation(schema)) {
94 final Class<?>[] classes = schema.subTypes();
95 final List<NamedType> names = new ArrayList<>(classes.length);
96 for (Class<?> subType : classes) {
97 names.add(new NamedType(subType));
98 }
99 if (!names.isEmpty()) {
100 return names;
101 }
102 }
103
104 return Collections.emptyList();
105 }
106
107 @Override
108 public String findTypeName(AnnotatedClass ac) {
109 io.swagger.v3.oas.annotations.media.Schema mp = AnnotationsUtils.getSchemaAnnotation(ac);
110
111 if (mp != null && !mp.name().isEmpty()) {
112 return mp.name();
113 }
114
115 return null;
116 }
117 }
118