Repository: swagger-api/swagger-parser
Pull request #1880 (2023-02-07 01:11:05)

fix for issue when dereferencing OneOf
fix for issue when dereferencing OneOf which has references in it.


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
index 6388d95b..286f9cca 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
@@ -149,8 +149,8 @@ public final class ExternalRefProcessor {
                     for(Schema item : composedSchema.getAllOf()){
                         if (item.get$ref() != null){
                             processRefSchema(item,file);
-                        } else if (item.getProperties() != null) {
-                            processProperties(item.getProperties(), file);
+                        } else{
+                            processSchema(item, file);
                         }
                     }
 
@@ -159,6 +159,8 @@ public final class ExternalRefProcessor {
                         if (item.get$ref() != null){
                             if (item.get$ref() != null){
                                 processRefSchema(item,file);
+                            }else{
+                                processSchema(item, file);
                             }
                         }
                     }
@@ -167,10 +169,11 @@ public final class ExternalRefProcessor {
                         if (item.get$ref() != null){
                             if (item.get$ref() != null){
                                 processRefSchema(item,file);
+                            }else{
+                                processSchema(item, file);
                             }
                         }
                     }
-
                 }
             }
             //Loop the properties and recursively call this method;


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
index 97eac48a..c46c320c 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
@@ -86,6 +86,18 @@ public class OpenAPIV3ParserTest {
     protected int serverPort = getDynamicPort();
     protected WireMockServer wireMockServer;
 
+
+    @Test
+    public void testIssueDereferencingComposedSchemaOneOf() {
+        OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult parseResult = openApiParser.readLocation("resolverIssue/Beta-1-swagger.yaml", null, options);
+        OpenAPI openAPI = parseResult.getOpenAPI();
+        assertNotNull(openAPI.getComponents().getSchemas().get("naNumber"));
+        assertNotNull(openAPI.getComponents().getSchemas().get("unNumber"));
+    }
+
     @Test
     public void testIssue1621() {
         final ParseOptions parseOptions = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml b/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml
new file mode 100644
index 00000000..b6c00d21
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml
@@ -0,0 +1,18 @@
+openapi: 3.0.3
+paths:
+  /v2/bookings:
+    post:
+      tags:
+       - Booking Request
+      summary: Post a booking request
+      description: |
+        Creates a new booking request
+      requestBody:
+        description: Parameters used to create a booking request
+        required: true
+        content:
+          application/json:
+            schema:
+              allOf:
+                - $ref: 'DOCUMENTATION_DOMAIN-3.0.0-domain.yaml#/components/schemas/dangerousGood'
+components:
diff --git a/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml b/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml
new file mode 100644
index 00000000..e202fbd7
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml
@@ -0,0 +1,29 @@
+openapi: 3.0.3
+paths:
+components:
+  schemas:
+    dangerousGood:
+      type: object
+      description: 'Specification for Dangerous Goods'
+      allOf:
+        - type: object
+          oneOf:
+            - type: object
+              properties:
+                unNumber:
+                  "$ref": "#/components/schemas/unNumber"
+            - type: object
+              properties:
+                naNumber:
+                  "$ref": "#/components/schemas/naNumber"
+    naNumber:
+      type: string
+      pattern: "\\d{4}"
+      description: 'Four-digit number that is assigned to dangerous, hazardous, and
+          harmful substances. Used for `US`/ `CFR` regularted shipments this is used
+          instead of `unNumber`'
+    unNumber:
+      type: string
+      pattern: "\\d{4}"
+      description: 'Four-digit number that is assigned to dangerous, hazardous, and
+                         harmful substances by United Nations'
\ No newline at end of file