Skip to content

issue-4850: fix extensions 'x-' prefix decoration for oas 3.1 #4880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
if (resolvedArrayAnnotation == null) {
schemaRefFromAnnotation = resolvedSchemaAnnotation.ref();
if (!openapi31) {
return new JsonSchema().$ref(resolvedSchemaAnnotation.ref()).name(name);
return new Schema().$ref(resolvedSchemaAnnotation.ref()).name(name);
}
} else {
ArraySchema schema = new ArraySchema();
Expand Down Expand Up @@ -578,9 +578,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
model = context.resolve(aType);
return model;
} else {
model = new Schema().name(name);
if (
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
model = openapi31 ? new JsonSchema().name(name) : new Schema().name(name);
if ((openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
if (openapi31 && resolvedArrayAnnotation == null) {
model.addType("object");
Expand Down Expand Up @@ -2604,7 +2603,8 @@ protected Map<String, Object> resolveExtensions(Annotated a, Annotation[] annota
if (schema != null &&
schema.extensions() != null &&
schema.extensions().length > 0) {
return AnnotationsUtils.getExtensions(openapi31, schema.extensions());
boolean usePrefix = !openapi31;
return AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
}
return null;
}
Expand Down Expand Up @@ -2798,7 +2798,8 @@ protected Map<String, Object> resolveExtensions(AnnotatedType a, io.swagger.v3.o
if (arraySchema != null &&
arraySchema.extensions() != null &&
arraySchema.extensions().length > 0) {
return AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions());
boolean usePrefix = !openapi31;
return AnnotationsUtils.getExtensions(openapi31, usePrefix, arraySchema.extensions());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
}

if (arraySchema.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions());
boolean usePrefix = !openapi31;
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, usePrefix, arraySchema.extensions());
if (extensions != null) {
extensions.forEach(arraySchemaObject::addExtension);
}
Expand Down Expand Up @@ -831,7 +832,8 @@ public static Optional<Schema> getSchemaFromAnnotation(
}

if (schema.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, schema.extensions());
boolean usePrefix = !openapi31;
Map<String, Object> extensions = AnnotationsUtils.getExtensions(openapi31, usePrefix, schema.extensions());
if (extensions != null) {
extensions.forEach(schemaObject::addExtension);
}
Expand Down Expand Up @@ -1847,20 +1849,26 @@ public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnota
}

public static Map<String, Object> getExtensions(Extension... extensions) {
return getExtensions(false, extensions);
return getExtensions(false, true, extensions);
}

public static Map<String, Object> getExtensions(boolean openapi31, Extension... extensions) {
return getExtensions(openapi31, true, extensions);
}

public static Map<String, Object> getExtensions(boolean openapi31, boolean usePrefix, Extension... extensions) {
final Map<String, Object> map = new HashMap<>();
for (Extension extension : extensions) {
final String name = extension.name();
String decoratedName = openapi31 ? name : StringUtils.prependIfMissing(name, "x-");
final String key = name.length() > 0 ? decoratedName : name;
String decoratedName = usePrefix
? StringUtils.prependIfMissing(name, "x-")
: name;
final String key = name.isEmpty() ? name : decoratedName;

for (ExtensionProperty property : extension.properties()) {
final String propertyName = property.name();
final String propertyValue = property.value();
JsonNode processedValue = null;
JsonNode processedValue;
final boolean propertyAsJson = property.parseValue();
if (StringUtils.isNotBlank(propertyName) && StringUtils.isNotBlank(propertyValue)) {
if (key.isEmpty()) {
Expand All @@ -1871,14 +1879,14 @@ public static Map<String, Object> getExtensions(boolean openapi31, Extension...
} else {
processedValue = Json.mapper().readTree(propertyValue);
}
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
map.put(decoratedName, processedValue);
} catch (Exception e) {
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
map.put(decoratedName, propertyValue);
}
} else {
decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-");
decoratedName = usePrefix ? StringUtils.prependIfMissing(propertyName, "x-") : propertyName;
map.put(decoratedName, propertyValue);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import io.swagger.v3.jaxrs2.resources.Ticket4804NotBlankResource;
import io.swagger.v3.jaxrs2.resources.Ticket4804ProcessorResource;
import io.swagger.v3.jaxrs2.resources.Ticket4804Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4850Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4859Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4879Resource;
import io.swagger.v3.jaxrs2.resources.UploadResource;
Expand Down Expand Up @@ -5342,4 +5343,37 @@ public void testTicket4065() {
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
ModelConverters.reset();
}

@Test(description = "Extensions Tests OAS 3.1")
public void testExtensionsOAS31() {
SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true);
Reader reader = new Reader(config);

OpenAPI openAPI = reader.read(Ticket4850Resource.class);
assertNotNull(openAPI);

String yaml = "openapi: 3.1.0\n" +
"paths:\n" +
" /bar:\n" +
" get:\n" +
" operationId: test\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" $ref: \"#/components/schemas/ExtensionsResource\"\n" +
"components:\n" +
" schemas:\n" +
" ExtensionsResource:\n" +
" description: ExtensionsResource\n" +
" x-user:\n" +
" name: Josh\n" +
" user-extensions:\n" +
" lastName: Hart\n" +
" address: House";
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.swagger.v3.jaxrs2.resources;

import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/bar")
public class Ticket4850Resource {
@GET
@Path("")
public ExtensionsResource test(

) {return new ExtensionsResource();}

@Schema(
description = "ExtensionsResource",
extensions = {
@Extension(name = "x-user", properties = {
@ExtensionProperty(name = "name", value = "Josh")}),
@Extension(name = "user-extensions", properties = {
@ExtensionProperty(name = "lastName", value = "Hart"),
@ExtensionProperty(name = "address", value = "House")})
}
)
private class ExtensionsResource {
public ExtensionsResource() {}
}
}
Loading