Skip to content

Commit eb1dc70

Browse files
committed
revert previous breaking changes, introduce backward compatibility solution including extra param to determine prefix addition
1 parent b7a1471 commit eb1dc70

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import io.swagger.v3.oas.annotations.media.SchemaProperty;
5454
import io.swagger.v3.oas.models.Components;
5555
import io.swagger.v3.oas.models.ExternalDocumentation;
56+
import io.swagger.v3.oas.models.SpecVersion;
5657
import io.swagger.v3.oas.models.media.ArraySchema;
5758
import io.swagger.v3.oas.models.media.ComposedSchema;
5859
import io.swagger.v3.oas.models.media.Discriminator;
@@ -578,9 +579,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
578579
model = context.resolve(aType);
579580
return model;
580581
} else {
581-
model = new Schema().name(name);
582-
if (
583-
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
582+
model = openapi31 ? new JsonSchema().name(name) : new Schema().name(name);
583+
if ((openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
584584
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
585585
if (openapi31 && resolvedArrayAnnotation == null) {
586586
model.addType("object");
@@ -2604,7 +2604,8 @@ protected Map<String, Object> resolveExtensions(Annotated a, Annotation[] annota
26042604
if (schema != null &&
26052605
schema.extensions() != null &&
26062606
schema.extensions().length > 0) {
2607-
return AnnotationsUtils.getExtensions(openapi31, schema.extensions());
2607+
boolean usePrefix = !openapi31;
2608+
return AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
26082609
}
26092610
return null;
26102611
}
@@ -2798,7 +2799,7 @@ protected Map<String, Object> resolveExtensions(AnnotatedType a, io.swagger.v3.o
27982799
if (arraySchema != null &&
27992800
arraySchema.extensions() != null &&
28002801
arraySchema.extensions().length > 0) {
2801-
return AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions());
2802+
return AnnotationsUtils.getExtensions(openapi31, false, arraySchema.extensions());
28022803
}
28032804
return null;
28042805
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ 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+
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, false, arraySchema.extensions());
543543
if (extensions != null) {
544544
extensions.forEach(arraySchemaObject::addExtension);
545545
}
@@ -831,7 +831,8 @@ public static Optional<Schema> getSchemaFromAnnotation(
831831
}
832832

833833
if (schema.extensions().length > 0) {
834-
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, schema.extensions());
834+
boolean usePrefix = !openapi31;
835+
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
835836
if (extensions != null) {
836837
extensions.forEach(schemaObject::addExtension);
837838
}
@@ -1847,16 +1848,21 @@ public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnota
18471848
}
18481849

18491850
public static Map<String, Object> getExtensions(Extension... extensions) {
1850-
return getExtensions(false, extensions);
1851+
return getExtensions(false, true, extensions);
18511852
}
18521853

18531854
public static Map<String, Object> getExtensions(boolean openapi31, Extension... extensions) {
1855+
return getExtensions(openapi31, true, extensions);
1856+
}
1857+
1858+
public static Map<String, Object> getExtensions(boolean openapi31, boolean usePrefix, Extension... extensions) {
18541859
final Map<String, Object> map = new HashMap<>();
18551860
for (Extension extension : extensions) {
18561861
final String name = extension.name();
1857-
String decoratedName = StringUtils.prependIfMissing(name, "x-");
1858-
1859-
final String key = !name.isEmpty() ? decoratedName : name;
1862+
String decoratedName = usePrefix
1863+
? StringUtils.prependIfMissing(name, "x-")
1864+
: name;
1865+
final String key = name.isEmpty() ? name : decoratedName;
18601866

18611867
for (ExtensionProperty property : extension.properties()) {
18621868
final String propertyName = property.name();
@@ -1872,14 +1878,14 @@ public static Map<String, Object> getExtensions(boolean openapi31, Extension...
18721878
} else {
18731879
processedValue = Json.mapper().readTree(propertyValue);
18741880
}
1875-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1881+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18761882
map.put(decoratedName, processedValue);
18771883
} catch (Exception e) {
1878-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1884+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18791885
map.put(decoratedName, propertyValue);
18801886
}
18811887
} else {
1882-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1888+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18831889
map.put(decoratedName, propertyValue);
18841890
}
18851891
} else {

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

Lines changed: 37 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;
@@ -149,13 +150,15 @@
149150
import java.util.Arrays;
150151
import java.util.HashMap;
151152
import java.util.HashSet;
153+
import java.util.LinkedHashSet;
152154
import java.util.List;
153155
import java.util.Map;
154156
import java.util.Optional;
155157
import java.util.Set;
156158
import java.util.concurrent.CompletableFuture;
157159
import java.util.concurrent.CopyOnWriteArrayList;
158160

161+
import static io.swagger.v3.oas.models.SpecVersion.V31;
159162
import static org.testng.Assert.assertEquals;
160163
import static org.testng.Assert.assertFalse;
161164
import static org.testng.Assert.assertNotEquals;
@@ -5342,4 +5345,38 @@ public void testTicket4065() {
53425345
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
53435346
ModelConverters.reset();
53445347
}
5348+
5349+
@Test(description = "Extensions Tests OAS 3.1")
5350+
public void testExtensionsOAS31() {
5351+
OpenAPI o = new OpenAPI(V31);
5352+
SwaggerConfiguration config = new SwaggerConfiguration().openAPI(o).openAPI31(true);
5353+
Reader reader = new Reader(o, new Paths(), new LinkedHashSet<>(), new Components(), config);
5354+
5355+
OpenAPI openAPI = reader.read(Ticket4850Resource.class);
5356+
assertNotNull(openAPI);
5357+
5358+
String yaml = "openapi: 3.1.0\n" +
5359+
"paths:\n" +
5360+
" /bar:\n" +
5361+
" get:\n" +
5362+
" operationId: test\n" +
5363+
" responses:\n" +
5364+
" default:\n" +
5365+
" description: default response\n" +
5366+
" content:\n" +
5367+
" '*/*':\n" +
5368+
" schema:\n" +
5369+
" $ref: \"#/components/schemas/ExtensionsResource\"\n" +
5370+
"components:\n" +
5371+
" schemas:\n" +
5372+
" ExtensionsResource:\n" +
5373+
" description: ExtensionsResource\n" +
5374+
" x-user:\n" +
5375+
" name: Josh\n" +
5376+
" user-extensions:\n" +
5377+
" lastName: Hart\n" +
5378+
" address: House";
5379+
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
5380+
}
5381+
53455382
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
import javax.ws.rs.Produces;
10+
11+
@Path("/bar")
12+
public class Ticket4850Resource {
13+
@GET
14+
@Path("")
15+
public ExtensionsResource test(
16+
17+
) {return new ExtensionsResource();}
18+
19+
@Schema(
20+
description = "ExtensionsResource",
21+
extensions = {
22+
@Extension(name = "x-user", properties = {
23+
@ExtensionProperty(name = "name", value = "Josh")}),
24+
@Extension(name = "user-extensions", properties = {
25+
@ExtensionProperty(name = "lastName", value = "Hart"),
26+
@ExtensionProperty(name = "address", value = "House")})
27+
}
28+
)
29+
private class ExtensionsResource {
30+
public ExtensionsResource() {}
31+
}
32+
}

0 commit comments

Comments
 (0)