From 05f9129cbe1e242c436fcb9f0d606362edf38bc1 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Fri, 25 Aug 2023 23:57:41 -0500 Subject: [PATCH] allow null values for extensions properties in openapi object --- .../v3/core/jackson/mixin/OpenAPIMixin.java | 2 ++ .../serialization/JsonSerializationTest.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/OpenAPIMixin.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/OpenAPIMixin.java index b2296e8b39..d495abf326 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/OpenAPIMixin.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/mixin/OpenAPIMixin.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import io.swagger.v3.core.jackson.PathsSerializer; import io.swagger.v3.oas.models.PathItem; @@ -12,6 +13,7 @@ public abstract class OpenAPIMixin { @JsonAnyGetter + @JsonInclude public abstract Map getExtensions(); @JsonAnySetter diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java index 2cc3c5deab..29163d0cca 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java @@ -18,6 +18,9 @@ import org.testng.annotations.Test; import org.yaml.snakeyaml.LoaderOptions; +import java.util.HashMap; +import java.util.Map; + import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; @@ -55,6 +58,19 @@ public void testExtension() throws Exception { } + @Test + public void testExtensionObjectWithProperties() throws Exception { + final Map extensionObjectProps = new HashMap<>(); + extensionObjectProps.put("x-foo-bar", "foo bar"); + extensionObjectProps.put("x-bar-foo", null); + + OpenAPI swagger = new OpenAPI(); + swagger.addExtension("x-extension-with-properties", extensionObjectProps); + + String swaggerJson = Json.mapper().writeValueAsString(swagger); + assertEquals(swaggerJson, "{\"openapi\":\"3.0.1\",\"x-extension-with-properties\":{\"x-foo-bar\":\"foo bar\",\"x-bar-foo\":null}}"); + } + @Test public void testSerializeASpecWithResponseReferences() throws Exception { OpenAPI swagger = new OpenAPI()