Skip to content

Commit bc7436e

Browse files
authored
Merge pull request #4880
refs 4850: fix extensions 'x-' prefix decoration for oas 3.1
2 parents 16638e3 + fe57655 commit bc7436e

File tree

4 files changed

+89
-15
lines changed

4 files changed

+89
-15
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
226226
if (resolvedArrayAnnotation == null) {
227227
schemaRefFromAnnotation = resolvedSchemaAnnotation.ref();
228228
if (!openapi31) {
229-
return new JsonSchema().$ref(resolvedSchemaAnnotation.ref()).name(name);
229+
return new Schema().$ref(resolvedSchemaAnnotation.ref()).name(name);
230230
}
231231
} else {
232232
ArraySchema schema = new ArraySchema();
@@ -578,9 +578,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
578578
model = context.resolve(aType);
579579
return model;
580580
} else {
581-
model = new Schema().name(name);
582-
if (
583-
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
581+
model = openapi31 ? new JsonSchema().name(name) : new Schema().name(name);
582+
if ((openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
584583
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
585584
if (openapi31 && resolvedArrayAnnotation == null) {
586585
model.addType("object");
@@ -2604,7 +2603,8 @@ protected Map<String, Object> resolveExtensions(Annotated a, Annotation[] annota
26042603
if (schema != null &&
26052604
schema.extensions() != null &&
26062605
schema.extensions().length > 0) {
2607-
return AnnotationsUtils.getExtensions(openapi31, schema.extensions());
2606+
boolean usePrefix = !openapi31;
2607+
return AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
26082608
}
26092609
return null;
26102610
}
@@ -2798,7 +2798,8 @@ protected Map<String, Object> resolveExtensions(AnnotatedType a, io.swagger.v3.o
27982798
if (arraySchema != null &&
27992799
arraySchema.extensions() != null &&
28002800
arraySchema.extensions().length > 0) {
2801-
return AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions());
2801+
boolean usePrefix = !openapi31;
2802+
return AnnotationsUtils.getExtensions(openapi31, usePrefix, arraySchema.extensions());
28022803
}
28032804
return null;
28042805
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
539539
}
540540

541541
if (arraySchema.extensions().length > 0) {
542-
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions());
542+
boolean usePrefix = !openapi31;
543+
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, usePrefix, arraySchema.extensions());
543544
if (extensions != null) {
544545
extensions.forEach(arraySchemaObject::addExtension);
545546
}
@@ -831,7 +832,8 @@ public static Optional<Schema> getSchemaFromAnnotation(
831832
}
832833

833834
if (schema.extensions().length > 0) {
834-
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, schema.extensions());
835+
boolean usePrefix = !openapi31;
836+
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
835837
if (extensions != null) {
836838
extensions.forEach(schemaObject::addExtension);
837839
}
@@ -1847,20 +1849,26 @@ public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnota
18471849
}
18481850

18491851
public static Map<String, Object> getExtensions(Extension... extensions) {
1850-
return getExtensions(false, extensions);
1852+
return getExtensions(false, true, extensions);
18511853
}
18521854

18531855
public static Map<String, Object> getExtensions(boolean openapi31, Extension... extensions) {
1856+
return getExtensions(openapi31, true, extensions);
1857+
}
1858+
1859+
public static Map<String, Object> getExtensions(boolean openapi31, boolean usePrefix, Extension... extensions) {
18541860
final Map<String, Object> map = new HashMap<>();
18551861
for (Extension extension : extensions) {
18561862
final String name = extension.name();
1857-
String decoratedName = openapi31 ? name : StringUtils.prependIfMissing(name, "x-");
1858-
final String key = name.length() > 0 ? decoratedName : name;
1863+
String decoratedName = usePrefix
1864+
? StringUtils.prependIfMissing(name, "x-")
1865+
: name;
1866+
final String key = name.isEmpty() ? name : decoratedName;
18591867

18601868
for (ExtensionProperty property : extension.properties()) {
18611869
final String propertyName = property.name();
18621870
final String propertyValue = property.value();
1863-
JsonNode processedValue = null;
1871+
JsonNode processedValue;
18641872
final boolean propertyAsJson = property.parseValue();
18651873
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
18661874
if (key.isEmpty()) {
@@ -1871,14 +1879,14 @@ public static Map<String, Object> getExtensions(boolean openapi31, Extension...
18711879
} else {
18721880
processedValue = Json.mapper().readTree(propertyValue);
18731881
}
1874-
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
1882+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18751883
map.put(decoratedName, processedValue);
18761884
} catch (Exception e) {
1877-
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
1885+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18781886
map.put(decoratedName, propertyValue);
18791887
}
18801888
} else {
1881-
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
1889+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18821890
map.put(decoratedName, propertyValue);
18831891
}
18841892
} else {

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import io.swagger.v3.jaxrs2.resources.Ticket4804NotBlankResource;
9191
import io.swagger.v3.jaxrs2.resources.Ticket4804ProcessorResource;
9292
import io.swagger.v3.jaxrs2.resources.Ticket4804Resource;
93+
import io.swagger.v3.jaxrs2.resources.Ticket4850Resource;
9394
import io.swagger.v3.jaxrs2.resources.Ticket4859Resource;
9495
import io.swagger.v3.jaxrs2.resources.Ticket4879Resource;
9596
import io.swagger.v3.jaxrs2.resources.UploadResource;
@@ -5342,4 +5343,37 @@ public void testTicket4065() {
53425343
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
53435344
ModelConverters.reset();
53445345
}
5346+
5347+
@Test(description = "Extensions Tests OAS 3.1")
5348+
public void testExtensionsOAS31() {
5349+
SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true);
5350+
Reader reader = new Reader(config);
5351+
5352+
OpenAPI openAPI = reader.read(Ticket4850Resource.class);
5353+
assertNotNull(openAPI);
5354+
5355+
String yaml = "openapi: 3.1.0\n" +
5356+
"paths:\n" +
5357+
" /bar:\n" +
5358+
" get:\n" +
5359+
" operationId: test\n" +
5360+
" responses:\n" +
5361+
" default:\n" +
5362+
" description: default response\n" +
5363+
" content:\n" +
5364+
" '*/*':\n" +
5365+
" schema:\n" +
5366+
" $ref: \"#/components/schemas/ExtensionsResource\"\n" +
5367+
"components:\n" +
5368+
" schemas:\n" +
5369+
" ExtensionsResource:\n" +
5370+
" description: ExtensionsResource\n" +
5371+
" x-user:\n" +
5372+
" name: Josh\n" +
5373+
" user-extensions:\n" +
5374+
" lastName: Hart\n" +
5375+
" address: House";
5376+
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
5377+
}
5378+
53455379
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.swagger.v3.jaxrs2.resources;
2+
3+
import io.swagger.v3.oas.annotations.extensions.Extension;
4+
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.Path;
9+
10+
@Path("/bar")
11+
public class Ticket4850Resource {
12+
@GET
13+
@Path("")
14+
public ExtensionsResource test(
15+
16+
) {return new ExtensionsResource();}
17+
18+
@Schema(
19+
description = "ExtensionsResource",
20+
extensions = {
21+
@Extension(name = "x-user", properties = {
22+
@ExtensionProperty(name = "name", value = "Josh")}),
23+
@Extension(name = "user-extensions", properties = {
24+
@ExtensionProperty(name = "lastName", value = "Hart"),
25+
@ExtensionProperty(name = "address", value = "House")})
26+
}
27+
)
28+
private class ExtensionsResource {
29+
public ExtensionsResource() {}
30+
}
31+
}

0 commit comments

Comments
 (0)