From fd61120863152522e0e6fddb905808f267247c9b Mon Sep 17 00:00:00 2001 From: frantuma Date: Mon, 3 Jul 2023 18:30:18 +0200 Subject: [PATCH] fix oas 3.1 extensions naming resolving --- .../io/swagger/v3/core/jackson/ModelResolver.java | 4 ++-- .../io/swagger/v3/core/util/AnnotationsUtils.java | 14 +++++++++----- .../swagger/v3/core/util/ParameterProcessor.java | 2 +- .../java/io/swagger/v3/jaxrs2/OperationParser.java | 4 ++-- .../src/main/java/io/swagger/v3/jaxrs2/Reader.java | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index bf836605a0..dc1a237ca3 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -2152,7 +2152,7 @@ protected Map resolveExtensions(Annotated a, Annotation[] annota if (schema != null && schema.extensions() != null && schema.extensions().length > 0) { - return AnnotationsUtils.getExtensions(schema.extensions()); + return AnnotationsUtils.getExtensions(openapi31, schema.extensions()); } return null; } @@ -2346,7 +2346,7 @@ protected Map resolveExtensions(AnnotatedType a, io.swagger.v3.o if (arraySchema != null && arraySchema.extensions() != null && arraySchema.extensions().length > 0) { - return AnnotationsUtils.getExtensions(arraySchema.extensions()); + return AnnotationsUtils.getExtensions(openapi31, arraySchema.extensions()); } return null; } diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java index 2986b3287e..679a07c864 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java @@ -802,7 +802,7 @@ public static Optional getSchemaFromAnnotation( } if (schema.extensions().length > 0) { - Map extensions = AnnotationsUtils.getExtensions(schema.extensions()); + Map extensions = AnnotationsUtils.getExtensions(openapi31, schema.extensions()); if (extensions != null) { extensions.forEach(schemaObject::addExtension); } @@ -1739,7 +1739,8 @@ public static Map getExtensions(boolean openapi31, Extension... final Map map = new HashMap<>(); for (Extension extension : extensions) { final String name = extension.name(); - final String key = name.length() > 0 ? StringUtils.prependIfMissing(name, "x-") : name; + String decoratedName = openapi31 ? name : StringUtils.prependIfMissing(name, "x-"); + final String key = name.length() > 0 ? decoratedName : name; for (ExtensionProperty property : extension.properties()) { final String propertyName = property.name(); @@ -1755,12 +1756,15 @@ public static Map getExtensions(boolean openapi31, Extension... } else { processedValue = Json.mapper().readTree(propertyValue); } - map.put(StringUtils.prependIfMissing(propertyName, "x-"), processedValue); + decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-"); + map.put(decoratedName, processedValue); } catch (Exception e) { - map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue); + decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-"); + map.put(decoratedName, propertyValue); } } else { - map.put(StringUtils.prependIfMissing(propertyName, "x-"), propertyValue); + decoratedName = openapi31 ? propertyName : StringUtils.prependIfMissing(propertyName, "x-"); + map.put(decoratedName, propertyValue); } } else { Object value = map.get(key); diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java index de969ff72c..e49ae8cc1a 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java @@ -154,7 +154,7 @@ public static Parameter applyAnnotations( } if (p.extensions().length > 0) { - Map extensionMap = AnnotationsUtils.getExtensions(p.extensions()); + Map extensionMap = AnnotationsUtils.getExtensions(openapi31, p.extensions()); if (extensionMap != null && ! extensionMap.isEmpty()) { extensionMap.forEach(parameter::addExtension); } diff --git a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java index 6ac39cea69..360a33ac14 100644 --- a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java +++ b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java @@ -42,7 +42,7 @@ public static Optional getRequestBody(io.swagger.v3.oas.annotations isEmpty = false; } if (requestBody.extensions().length > 0) { - Map extensions = AnnotationsUtils.getExtensions(requestBody.extensions()); + Map extensions = AnnotationsUtils.getExtensions(openapi31, requestBody.extensions()); if (extensions != null) { extensions.forEach(requestBodyObject::addExtension); } @@ -85,7 +85,7 @@ public static Optional getApiResponses(final io.swagger.v3.oas.ann apiResponseObject.setDescription(response.description()); } if (response.extensions().length > 0) { - Map extensions = AnnotationsUtils.getExtensions(response.extensions()); + Map extensions = AnnotationsUtils.getExtensions(openapi31, response.extensions()); if (extensions != null) { extensions.forEach(apiResponseObject::addExtension); } diff --git a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java index a4e4e0ff53..be60e5f8af 100644 --- a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java +++ b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java @@ -335,7 +335,7 @@ public OpenAPI read(Class cls, // OpenApiDefinition extensions if (openAPIDefinition.extensions().length > 0) { openAPI.setExtensions(AnnotationsUtils - .getExtensions(openAPIDefinition.extensions())); + .getExtensions(config.isOpenAPI31(), openAPIDefinition.extensions())); } }