Repository: swagger-api/swagger-parser
Pull request #1795 (2022-09-13 13:42:17)

Issue 1454
It fixes only one of the failing cases of the provided tests though.
I didn't consider cases where the default value is defined on the enum and expected to be inherited by the property.


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java
index 0b592af4..a7423022 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java
@@ -380,16 +380,21 @@ public class ResolverFully {
 
             if (aggregateCombinators && (hasAllOf || adjacent)) {
                 Schema combinedModel = SchemaTypeUtil.createSchema(composedSchema.getType(), composedSchema.getFormat());
+//                combinedModel.setDefault(composedSchema.getDefault());
                 Set<Object> examples = new HashSet<>();
+                Set<Object> defaultValues = new HashSet<>();
 
                 if (hasAllOf) {
-                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAllOf(), examples);
+                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAllOf(), examples, defaultValues);
                 }
                 if (hasOneOf) {
-                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getOneOf(), examples);
+                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getOneOf(), examples, defaultValues);
                 }
                 if (hasAnyOf) {
-                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAnyOf(), examples);
+                    aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAnyOf(), examples, defaultValues);
+                }
+                if (defaultValues.size() == 1) {
+                    combinedModel.setDefault(defaultValues.iterator().next());
                 }
 
                 if (schema.getExample() != null) {
@@ -469,10 +474,9 @@ public class ResolverFully {
     }
 
     private void aggregateSchemaCombinators(ComposedSchema sourceSchema, Schema targetSchema,
-                                            List<Schema> schemasToAggregate, Set<Object> examples) {
+                                            List<Schema> schemasToAggregate, Set<Object> examples, Set<Object> defaultValues) {
 
         Set<String> requiredProperties = new HashSet<>();
-
         for (Schema innerModel : schemasToAggregate) {
             Schema resolved = resolveSchema(innerModel);
             Map<String, Schema> properties = resolved.getProperties();
@@ -496,6 +500,11 @@ public class ResolverFully {
             if (resolved.getExample() != null) {
                 examples.add(resolved.getExample());
             }
+            if (sourceSchema.getDefault() != null && resolved.getDefault() == null)
+                defaultValues.add(sourceSchema.getDefault());
+            else
+                defaultValues.add(resolved.getDefault());
+
             if (resolved.getExtensions() != null) {
                 Map<String, Object> extensions = resolved.getExtensions();
                 for (String key : extensions.keySet()) {


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
index d63f73c8..2cadef7b 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
@@ -2696,6 +2696,261 @@ public class OpenAPIDeserializerTest {
         assertNull( mediaType.getExample(), "Example,");
     }
 
+    @Test
+    public void testIssue1454AllOfDefaultValue(){
+        String json =
+                "{" +
+                "  \"openapi\": \"3.0.1\"," +
+                "  \"info\": {" +
+                "    \"title\": \"Here be enum bugs\"," +
+                "    \"description\": \"OpenAPI spec to test the enum parsing bugs.\"," +
+                "    \"version\": \"1.0.0\"" +
+                "  }," +
+                "  \"paths\": {" +
+                "    \"/enum/parse/bug\": {" +
+                "      \"post\": {" +
+                "        \"summary\": \"Test enum parse bug\"," +
+                "        \"requestBody\": {" +
+                "          \"content\": {" +
+                "            \"application/json\": {" +
+                "              \"schema\": {" +
+                "                \"$ref\": \"#/components/schemas/SchemaWithDefaultAndEnum\"" +
+                "              }" +
+                "            }" +
+                "          }" +
+                "        }," +
+                "        \"responses\": {" +
+                "          \"default\": {" +
+                "            \"description\": \"Successful response\"" +
+                "          }" +
+                "        }" +
+                "      }" +
+                "    }" +
+                "  }," +
+                "  \"components\": {" +
+                "    \"schemas\": {" +
+                "      \"SchemaWithDefaultAndEnum\": {" +
+                "        \"type\": \"object\"," +
+                "        \"properties\": {" +
+                "          \"enumProperty\": {" +
+                "            \"description\": \"This schema with set default enum value and enum ref shows default SCHEMA_DEFAULT value in Swagger Editor, but is null when parsed with OpenAPIV3Parser.\"," +
+                "            \"allOf\": [" +
+                "              {" +
+                "                \"$ref\": \"#/components/schemas/Enum\"" +
+                "              }" +
+                "            ]," +
+                "            \"default\": \"SCHEMA_DEFAULT\"" +
+                "          }" +
+                "        }" +
+                "      }," +
+                "      \"Enum\": {" +
+                "        \"type\": \"string\"," +
+                "        \"enum\": [" +
+                "          \"SCHEMA_DEFAULT\"," +
+                "          \"NOT_DEFAULT\"" +
+                "        ]" +
+                "      }" +
+                "    }" +
+                "  }" +
+                "}";
+
+
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+        SwaggerParseResult result = parser.readContents(json, null, null);
+        OpenAPI openAPI = result.getOpenAPI();
+        assertEquals(((Map<String, Schema>)openAPI.getComponents()
+                .getSchemas()
+                .get("SchemaWithDefaultAndEnum")
+                .getProperties())
+                .get("enumProperty")
+                .getDefault(), "SCHEMA_DEFAULT");
+
+        OpenAPIV3Parser parser2 = new OpenAPIV3Parser();
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        options.setResolveFully(true);
+        options.setResolveCombinators(true);
+        SwaggerParseResult result2 = parser2.readContents(json, null, options);
+        OpenAPI openAPI2 = result2.getOpenAPI();
+        assertEquals(((Map<String, Schema>)openAPI2.getComponents()
+                .getSchemas()
+                .get("SchemaWithDefaultAndEnum")
+                .getProperties())
+                .get("enumProperty")
+                .getDefault(), "SCHEMA_DEFAULT");
+    }
+
+    @Test
+    public void testIssue1454AllOfEnumWithDefaultValue(){
+        String json =
+                "{" +
+                "  \"openapi\": \"3.0.1\"," +
+                "  \"info\": {" +
+                "    \"title\": \"Here be enum bugs\"," +
+                "    \"description\": \"OpenAPI spec to test the enum parsing bugs.\"," +
+                "    \"version\": \"1.0.0\"" +
+                "  }," +
+                "  \"paths\": {" +
+                "    \"/enum/parse/bug\": {" +
+                "      \"post\": {" +
+                "        \"summary\": \"Test enum parse bug\"," +
+                "        \"requestBody\": {" +
+                "          \"content\": {" +
+                "            \"application/json\": {" +
+                "              \"schema\": {" +
+                "                \"$ref\": \"#/components/schemas/SchemaWithEnumWithDefault\"" +
+                "              }" +
+                "            }" +
+                "          }" +
+                "        }," +
+                "        \"responses\": {" +
+                "          \"default\": {" +
+                "            \"description\": \"Successful response\"" +
+                "          }" +
+                "        }" +
+                "      }" +
+                "    }" +
+                "  }," +
+                "  \"components\": {" +
+                "    \"schemas\": {" +
+                "      \"SchemaWithEnumWithDefault\": {" +
+                "        \"type\": \"object\"," +
+                "        \"properties\": {" +
+                "          \"enumProperty\": {" +
+                "            \"description\": \"This schema with set default enum value and enum ref shows default SCHEMA_DEFAULT value in Swagger Editor, but is null when parsed with OpenAPIV3Parser.\"," +
+                "            \"allOf\": [" +
+                "              {" +
+                "                \"$ref\": \"#/components/schemas/EnumWithDefault\"" +
+                "              }" +
+                "            ]" +
+                "          }" +
+                "        }" +
+                "      }," +
+                "      \"EnumWithDefault\": {" +
+                "        \"type\": \"string\"," +
+                "        \"enum\": [" +
+                "          \"SCHEMA_DEFAULT\"," +
+                "          \"NOT_DEFAULT\"" +
+                "        ]," +
+                "        \"default\": \"SCHEMA_DEFAULT\"" +
+                "      }" +
+                "    }" +
+                "  }" +
+                "}";
+
+        // it is really expected to be working in resolve=false mode?
+//        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+//        SwaggerParseResult result = parser.readContents(json, null, new ParseOptions());
+//        OpenAPI openAPI = result.getOpenAPI();
+//        assertEquals(((Map<String, Schema>)openAPI.getComponents()
+//                .getSchemas()
+//                .get("SchemaWithEnumWithDefault")
+//                .getProperties())
+//                .get("enumProperty")
+//                .getDefault(), "SCHEMA_DEFAULT");
+
+        OpenAPIV3Parser parser2 = new OpenAPIV3Parser();
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        options.setResolveFully(true);
+        options.setResolveCombinators(true);
+        SwaggerParseResult result2 = parser2.readContents(json, null, options);
+        OpenAPI openAPI2 = result2.getOpenAPI();
+        assertEquals(((Map<String, Schema>)openAPI2.getComponents()
+                .getSchemas()
+                .get("SchemaWithEnumWithDefault")
+                .getProperties())
+                .get("enumProperty")
+                .getDefault(), "SCHEMA_DEFAULT");
+
+    }
+
+    @Test
+    public void testIssue1454WithDefaultAllOfEnumWithDefaultValue(){
+        String json =
+                "{" +
+                "  \"openapi\": \"3.0.1\"," +
+                "  \"info\": {" +
+                "    \"title\": \"Here be enum bugs\"," +
+                "    \"description\": \"OpenAPI spec to test the enum parsing bugs.\"," +
+                "    \"version\": \"1.0.0\"" +
+                "  }," +
+                "  \"paths\": {" +
+                "    \"/enum/parse/bug\": {" +
+                "      \"post\": {" +
+                "        \"summary\": \"Test enum parse bug\"," +
+                "        \"requestBody\": {" +
+                "          \"content\": {" +
+                "            \"application/json\": {" +
+                "              \"schema\": {" +
+                "                \"$ref\": \"#/components/schemas/SchemaWithDefaultAndEnumWithDefault\"" +
+                "              }" +
+                "            }" +
+                "          }" +
+                "        }," +
+                "        \"responses\": {" +
+                "          \"default\": {" +
+                "            \"description\": \"Successful response\"" +
+                "          }" +
+                "        }" +
+                "      }" +
+                "    }" +
+                "  }," +
+                "  \"components\": {" +
+                "    \"schemas\": {" +
+                "      \"SchemaWithDefaultAndEnumWithDefault\": {" +
+                "        \"type\": \"object\"," +
+                "        \"properties\": {" +
+                "          \"enumProperty\": {" +
+                "            \"description\": \"This schema with set default enum value and enum ref shows default SCHEMA_DEFAULT value in Swagger Editor, but is null when parsed with OpenAPIV3Parser.\"," +
+                "            \"allOf\": [" +
+                "              {" +
+                "                \"$ref\": \"#/components/schemas/EnumWithDefault\"" +
+                "              }" +
+                "            ]," +
+                "            \"default\": \"SCHEMA_DEFAULT\"" +
+                "          }" +
+                "        }" +
+                "      }," +
+                "      \"EnumWithDefault\": {" +
+                "        \"type\": \"string\"," +
+                "        \"enum\": [" +
+                "          \"SCHEMA_DEFAULT\"," +
+                "          \"NOT_DEFAULT\"" +
+                "        ]," +
+                "        \"default\": \"SCHEMA_DEFAULT\"" +
+                "      }" +
+                "    }" +
+                "  }" +
+                "}";
+
+
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+        SwaggerParseResult result = parser.readContents(json, null, new ParseOptions());
+        OpenAPI openAPI = result.getOpenAPI();
+        assertEquals(((Map<String, Schema>)openAPI.getComponents()
+                .getSchemas()
+                .get("SchemaWithDefaultAndEnumWithDefault")
+                .getProperties())
+                .get("enumProperty")
+                .getDefault(), "SCHEMA_DEFAULT");
+
+        OpenAPIV3Parser parser2 = new OpenAPIV3Parser();
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        options.setResolveFully(true);
+        options.setResolveCombinators(true);
+        SwaggerParseResult result2 = parser2.readContents(json, null, options);
+        OpenAPI openAPI2 = result2.getOpenAPI();
+        assertEquals(((Map<String, Schema>)openAPI2.getComponents()
+                .getSchemas()
+                .get("SchemaWithDefaultAndEnumWithDefault")
+                .getProperties())
+                .get("enumProperty")
+                .getDefault(), "SCHEMA_DEFAULT");
+
+    }
+
     @Test
     public void testOptionalParameter(@Injectable List<AuthorizationValue> auths) {
         String yaml = "openapi: 3.0.1\n" +