Skip to content

Commit f18513f

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

File tree

4 files changed

+89
-14
lines changed

4 files changed

+89
-14
lines changed

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

Lines changed: 5 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,7 @@ 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+
return AnnotationsUtils.getExtensions(openapi31, false, schema.extensions());
26082608
}
26092609
return null;
26102610
}
@@ -2798,7 +2798,7 @@ 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+
return AnnotationsUtils.getExtensions(openapi31, false, arraySchema.extensions());
28022802
}
28032803
return null;
28042804
}

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

Lines changed: 14 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,7 @@ public static Optional<Schema> getSchemaFromAnnotation(
831831
}
832832

833833
if (schema.extensions().length > 0) {
834-
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, schema.extensions());
834+
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, false, schema.extensions());
835835
if (extensions != null) {
836836
extensions.forEach(schemaObject::addExtension);
837837
}
@@ -1847,16 +1847,21 @@ public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnota
18471847
}
18481848

18491849
public static Map<String, Object> getExtensions(Extension... extensions) {
1850-
return getExtensions(false, extensions);
1850+
return getExtensions(false, true, extensions);
18511851
}
18521852

18531853
public static Map<String, Object> getExtensions(boolean openapi31, Extension... extensions) {
1854+
return getExtensions(openapi31, true, extensions);
1855+
}
1856+
1857+
public static Map<String, Object> getExtensions(boolean openapi31, boolean usePrefix, Extension... extensions) {
18541858
final Map<String, Object> map = new HashMap<>();
18551859
for (Extension extension : extensions) {
18561860
final String name = extension.name();
1857-
String decoratedName = StringUtils.prependIfMissing(name, "x-");
1858-
1859-
final String key = !name.isEmpty() ? decoratedName : name;
1861+
String decoratedName = usePrefix
1862+
? StringUtils.prependIfMissing(name, "x-")
1863+
: name;
1864+
final String key = name.isEmpty() ? name : decoratedName;
18601865

18611866
for (ExtensionProperty property : extension.properties()) {
18621867
final String propertyName = property.name();
@@ -1872,14 +1877,14 @@ public static Map<String, Object> getExtensions(boolean openapi31, Extension...
18721877
} else {
18731878
processedValue = Json.mapper().readTree(propertyValue);
18741879
}
1875-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1880+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18761881
map.put(decoratedName, processedValue);
18771882
} catch (Exception e) {
1878-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1883+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18791884
map.put(decoratedName, propertyValue);
18801885
}
18811886
} else {
1882-
decoratedName = StringUtils.prependIfMissing(propertyName, "x-");
1887+
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
18831888
map.put(decoratedName, propertyValue);
18841889
}
18851890
} else {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@
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;
9697
import io.swagger.v3.jaxrs2.resources.UrlEncodedResourceWithEncodings;
9798
import io.swagger.v3.jaxrs2.resources.UserAnnotationResource;
9899
import io.swagger.v3.jaxrs2.resources.WebHookResource;
99100
import io.swagger.v3.jaxrs2.resources.extensions.ExtensionsResource;
101+
import io.swagger.v3.jaxrs2.resources.extensions.ExtensionsResource31;
100102
import io.swagger.v3.jaxrs2.resources.extensions.OperationExtensionsResource;
101103
import io.swagger.v3.jaxrs2.resources.extensions.ParameterExtensionsResource;
102104
import io.swagger.v3.jaxrs2.resources.extensions.RequestBodyExtensionsResource;
@@ -149,13 +151,15 @@
149151
import java.util.Arrays;
150152
import java.util.HashMap;
151153
import java.util.HashSet;
154+
import java.util.LinkedHashSet;
152155
import java.util.List;
153156
import java.util.Map;
154157
import java.util.Optional;
155158
import java.util.Set;
156159
import java.util.concurrent.CompletableFuture;
157160
import java.util.concurrent.CopyOnWriteArrayList;
158161

162+
import static io.swagger.v3.oas.models.SpecVersion.V31;
159163
import static org.testng.Assert.assertEquals;
160164
import static org.testng.Assert.assertFalse;
161165
import static org.testng.Assert.assertNotEquals;
@@ -5342,4 +5346,38 @@ public void testTicket4065() {
53425346
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
53435347
ModelConverters.reset();
53445348
}
5349+
5350+
@Test(description = "Extensions Tests OAS 3.1")
5351+
public void testExtensionsOAS31() {
5352+
OpenAPI o = new OpenAPI(V31);
5353+
SwaggerConfiguration config = new SwaggerConfiguration().openAPI(o).openAPI31(true);
5354+
Reader reader = new Reader(o, new Paths(), new LinkedHashSet<>(), new Components(), config);
5355+
5356+
OpenAPI openAPI = reader.read(Ticket4850Resource.class);
5357+
assertNotNull(openAPI);
5358+
5359+
String yaml = "openapi: 3.1.0\n" +
5360+
"paths:\n" +
5361+
" /bar:\n" +
5362+
" get:\n" +
5363+
" operationId: test\n" +
5364+
" responses:\n" +
5365+
" default:\n" +
5366+
" description: default response\n" +
5367+
" content:\n" +
5368+
" '*/*':\n" +
5369+
" schema:\n" +
5370+
" $ref: \"#/components/schemas/ExtensionsResource\"\n" +
5371+
"components:\n" +
5372+
" schemas:\n" +
5373+
" ExtensionsResource:\n" +
5374+
" description: ExtensionsResource\n" +
5375+
" x-user:\n" +
5376+
" name: Josh\n" +
5377+
" user-extensions:\n" +
5378+
" lastName: Hart\n" +
5379+
" address: House";
5380+
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
5381+
}
5382+
53455383
}
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)