|
25 | 25 | import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.MethodMeta;
|
26 | 26 | import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ParameterMeta;
|
27 | 27 | import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ServiceMeta;
|
| 28 | +import org.apache.dubbo.rpc.protocol.tri.rest.openapi.Constants; |
28 | 29 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPIDefinitionResolver;
|
29 | 30 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPISchemaPredicate;
|
30 | 31 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.OpenAPISchemaResolver;
|
|
35 | 36 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.OpenAPI;
|
36 | 37 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Operation;
|
37 | 38 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema;
|
| 39 | +import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema.Type; |
38 | 40 | import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Tag;
|
39 | 41 |
|
| 42 | +import java.util.Arrays; |
40 | 43 | import java.util.Map;
|
41 | 44 |
|
42 | 45 | import io.swagger.v3.oas.annotations.ExternalDocumentation;
|
43 | 46 | import io.swagger.v3.oas.annotations.Hidden;
|
44 | 47 | import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
45 | 48 | import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
|
| 49 | +import io.swagger.v3.oas.annotations.media.Schema.AccessMode; |
| 50 | +import io.swagger.v3.oas.annotations.media.Schema.RequiredMode; |
46 | 51 |
|
| 52 | +import static org.apache.dubbo.rpc.protocol.tri.rest.openapi.Helper.setValue; |
47 | 53 | import static org.apache.dubbo.rpc.protocol.tri.rest.openapi.Helper.trim;
|
48 | 54 |
|
49 | 55 | @Activate(order = 50, onClass = "io.swagger.v3.oas.annotations.OpenAPIDefinition")
|
@@ -98,7 +104,14 @@ public OpenAPI resolve(OpenAPI openAPI, ServiceMeta serviceMeta, OpenAPIChain ch
|
98 | 104 |
|
99 | 105 | openAPI.setExternalDocs(toExternalDocs(anno.externalDocs()));
|
100 | 106 |
|
101 |
| - openAPI.setExtensions(toProperties(anno.extensions())); |
| 107 | + Map<String, String> properties = toProperties(anno.extensions()); |
| 108 | + if (properties != null) { |
| 109 | + String group = properties.remove(Constants.X_API_GROUP); |
| 110 | + if (group != null) { |
| 111 | + openAPI.setGroup(group); |
| 112 | + } |
| 113 | + openAPI.setExtensions(properties); |
| 114 | + } |
102 | 115 | return openAPI;
|
103 | 116 | }
|
104 | 117 |
|
@@ -142,27 +155,74 @@ public Operation resolve(Operation operation, MethodMeta methodMeta, OperationCo
|
142 | 155 | for (String tag : anno.tags()) {
|
143 | 156 | operation.addTag(tag);
|
144 | 157 | }
|
| 158 | + Map<String, String> properties = toProperties(anno.extensions()); |
| 159 | + if (properties != null) { |
| 160 | + String group = properties.remove(Constants.X_API_GROUP); |
| 161 | + if (group != null) { |
| 162 | + operation.setGroup(group); |
| 163 | + } |
| 164 | + String version = properties.remove(Constants.X_API_VERSION); |
| 165 | + if (version != null) { |
| 166 | + operation.setVersion(version); |
| 167 | + } |
| 168 | + operation.setExtensions(properties); |
| 169 | + } |
145 | 170 | return operation
|
146 | 171 | .setSummary(trim(anno.summary()))
|
147 | 172 | .setDescription(trim(anno.description()))
|
148 | 173 | .setExternalDocs(toExternalDocs(anno.externalDocs()))
|
149 | 174 | .setOperationId(trim(anno.operationId()))
|
150 |
| - .setDeprecated(anno.deprecated() ? Boolean.TRUE : null) |
151 |
| - .setExtensions(toProperties(anno.extensions())); |
| 175 | + .setDeprecated(anno.deprecated() ? Boolean.TRUE : null); |
152 | 176 | }
|
153 | 177 |
|
154 | 178 | @Override
|
155 | 179 | public Schema resolve(ParameterMeta parameter, SchemaContext context, SchemaChain chain) {
|
156 |
| - AnnotationMeta<io.swagger.v3.oas.annotations.media.Schema> meta = |
| 180 | + AnnotationMeta<io.swagger.v3.oas.annotations.media.Schema> annoMeta = |
157 | 181 | parameter.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
|
158 |
| - if (meta == null) { |
| 182 | + if (annoMeta == null) { |
159 | 183 | return chain.resolve(parameter, context);
|
160 | 184 | }
|
161 |
| - io.swagger.v3.oas.annotations.media.Schema schema = meta.getAnnotation(); |
162 |
| - if (schema.hidden() || parameter.isHierarchyAnnotated(Hidden.class)) { |
| 185 | + io.swagger.v3.oas.annotations.media.Schema anno = annoMeta.getAnnotation(); |
| 186 | + if (anno.hidden() || parameter.isHierarchyAnnotated(Hidden.class)) { |
163 | 187 | return null;
|
164 | 188 | }
|
165 |
| - return chain.resolve(parameter, context); |
| 189 | + Schema schema = chain.resolve(parameter, context); |
| 190 | + if (schema == null) { |
| 191 | + return null; |
| 192 | + } |
| 193 | + |
| 194 | + Map<String, String> properties = toProperties(anno.extensions()); |
| 195 | + if (properties != null) { |
| 196 | + String group = properties.remove(Constants.X_API_GROUP); |
| 197 | + if (group != null) { |
| 198 | + schema.setGroup(group); |
| 199 | + } |
| 200 | + String version = properties.remove(Constants.X_API_VERSION); |
| 201 | + if (version != null) { |
| 202 | + schema.setVersion(version); |
| 203 | + } |
| 204 | + schema.setExtensions(properties); |
| 205 | + } |
| 206 | + |
| 207 | + setValue(anno::type, v -> schema.setType(Type.valueOf(v))); |
| 208 | + setValue(anno::format, schema::setFormat); |
| 209 | + setValue(anno::name, schema::setName); |
| 210 | + setValue(anno::title, schema::setTitle); |
| 211 | + setValue(anno::description, schema::setDescription); |
| 212 | + setValue(anno::defaultValue, schema::setDefaultValue); |
| 213 | + setValue(anno::pattern, schema::setPattern); |
| 214 | + setValue(anno::example, schema::setExample); |
| 215 | + String[] enumItems = trim(anno.allowableValues()); |
| 216 | + if (enumItems != null) { |
| 217 | + schema.setEnumeration(Arrays.asList(enumItems)); |
| 218 | + } |
| 219 | + schema.setRequired(anno.requiredMode() == RequiredMode.REQUIRED ? Boolean.TRUE : null); |
| 220 | + schema.setReadOnly(anno.accessMode() == AccessMode.READ_ONLY ? Boolean.TRUE : null); |
| 221 | + schema.setWriteOnly(anno.accessMode() == AccessMode.WRITE_ONLY ? Boolean.TRUE : null); |
| 222 | + schema.setNullable(anno.nullable() ? Boolean.TRUE : null); |
| 223 | + schema.setDeprecated(anno.deprecated() ? Boolean.TRUE : null); |
| 224 | + |
| 225 | + return schema; |
166 | 226 | }
|
167 | 227 |
|
168 | 228 | @Override
|
|
0 commit comments