Skip to content

Commit 5285854

Browse files
authored
Merge pull request #2725 from swagger-api/ticket-2723
refs #2723 - support for complex values in ExtensionProperty
2 parents d53f148 + 9718449 commit 5285854

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/extensions/ExtensionProperty.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@
2727
* @return the value of the property
2828
*/
2929
String value();
30+
31+
/**
32+
* If set to true, field `value` will be parsed and serialized as JSON/YAML
33+
*
34+
* @return the value of `parseValue` annotation field
35+
*/
36+
boolean parseValue() default false;
3037
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonView;
44
import com.fasterxml.jackson.databind.JavaType;
5+
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.introspect.Annotated;
67
import io.swagger.v3.core.converter.AnnotatedType;
78
import io.swagger.v3.core.converter.ModelConverters;
@@ -1176,17 +1177,37 @@ public static Map<String, Object> getExtensions(Extension... extensions) {
11761177
for (ExtensionProperty property : extension.properties()) {
11771178
final String propertyName = property.name();
11781179
final String propertyValue = property.value();
1180+
JsonNode processedValue = null;
1181+
final boolean propertyAsJson = property.parseValue();
11791182
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
11801183
if (key.isEmpty()) {
1181-
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
1184+
if (propertyAsJson) {
1185+
try {
1186+
processedValue = Json.mapper().readTree(propertyValue);
1187+
map.put(StringUtils.prependIfMissing(propertyName, "x-"), processedValue);
1188+
} catch (Exception e) {
1189+
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
1190+
}
1191+
} else {
1192+
map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue);
1193+
}
11821194
} else {
11831195
Object value = map.get(key);
11841196
if (value == null || !(value instanceof Map)) {
11851197
value = new HashMap<String, Object>();
11861198
map.put(key, value);
11871199
}
11881200
@SuppressWarnings("unchecked") final Map<String, Object> mapValue = (Map<String, Object>) value;
1189-
mapValue.put(propertyName, propertyValue);
1201+
if (propertyAsJson) {
1202+
try {
1203+
processedValue = Json.mapper().readTree(propertyValue);
1204+
mapValue.put(propertyName, processedValue);
1205+
} catch (Exception e) {
1206+
mapValue.put(propertyName, propertyValue);
1207+
}
1208+
} else {
1209+
mapValue.put(propertyName, propertyValue);
1210+
}
11901211
}
11911212
}
11921213
}

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/extensions/ExtensionsResource.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ public class ExtensionsResource {
165165
@ExtensionProperty(name = "name", value = "Josh")}),
166166
@Extension(name = "x-operation-extensions", properties = {
167167
@ExtensionProperty(name = "lastName", value = "Hart"),
168-
@ExtensionProperty(name = "address", value = "House")})
168+
@ExtensionProperty(name = "address", value = "House")}),
169+
@Extension(properties = {
170+
@ExtensionProperty(name = "codes", value = "[\"11\", \"12\"]", parseValue = true),
171+
@ExtensionProperty(name = "name", value = "Josh")})
169172
})
170173
public Response getSummaryAndDescription(
171174
@Parameter(
@@ -434,11 +437,15 @@ public ExtensionUser setUser(
434437
" - read:pets\n" +
435438
" - myOauth2Security:\n" +
436439
" - write:pets\n" +
440+
" x-name: Josh\n" +
437441
" x-operation:\n" +
438442
" name: Josh\n" +
439443
" x-operation-extensions:\n" +
440444
" lastName: Hart\n" +
441445
" address: House\n" +
446+
" x-codes:\n" +
447+
" - \"11\"\n" +
448+
" - \"12\"\n" +
442449
" /user:\n" +
443450
" get:\n" +
444451
" operationId: getUser\n" +

0 commit comments

Comments
 (0)