Repository: swagger-api/swagger-parser
Pull request #777 (2018-07-23 06:19:13)

Fix java.lang.IllegalArgumentException with allOf and discriminator
Replacement PR for #746 to add unit test (fix for issue #740).


Patch:
diff --git a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
index e94b9895..1fd9da28 100644
--- a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
+++ b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
@@ -1067,11 +1067,15 @@ public class SwaggerConverter implements SwaggerParserExtension {
             result = arraySchema;
         } else if (v2Model instanceof ComposedModel) {
             ComposedModel composedModel = (ComposedModel) v2Model;
-
-            ComposedSchema composed = Json.mapper().convertValue(v2Model, ComposedSchema.class);
-
+            ComposedSchema composed = new ComposedSchema();
+            composed.setDescription(composedModel.getDescription());
+            composed.setExample(composedModel.getExample());
+            if (composedModel.getExternalDocs() != null) {
+                composed.setExternalDocs(convert(composedModel.getExternalDocs()));
+            }
+            composed.setTitle(composedModel.getTitle());
+            composed.setExtensions(convert(composedModel.getVendorExtensions()));
             composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList()));
-
             result = composed;
         } else {
             String v2discriminator = null;
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
index c4bcd21a..0e6c3312 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
@@ -106,10 +106,12 @@ public class OpenAPIDeserializer {
     public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
         SwaggerParseResult result = new SwaggerParseResult();
         try {
+            
             ParseResult rootParse = new ParseResult();
             OpenAPI api = parseRoot(rootNode, rootParse, path);
             result.setOpenAPI(api);
             result.setMessages(rootParse.getMessages());
+
         } catch (Exception e) {
             result.setMessages(Arrays.asList(e.getMessage()));
 


Test patch:
diff --git a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
index c2458b1a..93c91a52 100644
--- a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
+++ b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
@@ -77,6 +77,7 @@ public class V2ConverterTest {
     private static final String ISSUE_673_YAML = "issue-673.yaml";
     private static final String ISSUE_676_JSON = "issue-676.json";
     private static final String ISSUE_708_YAML = "issue-708.yaml";
+    private static final String ISSUE_740_YAML = "issue-740.yaml";
     private static final String ISSUE_756_JSON = "issue-756.json";
     private static final String ISSUE_758_JSON = "issue-758.json";
     private static final String ISSUE_762_JSON = "issue-762.json";
@@ -619,6 +620,16 @@ public class V2ConverterTest {
         assertEquals(schema.getPattern(), "^[0-9]+$");
     }
 
+    @Test(description = "OpenAPI v2 converter - Migrate a schema with AllOf")
+    public void testIssue740() throws Exception {
+        final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_740_YAML);
+        assertNotNull(oas);
+        Schema schema = oas.getComponents().getSchemas().get("Action");
+        assertTrue(schema instanceof ComposedSchema);
+        ComposedSchema composedSchema = (ComposedSchema) schema;
+        assertEquals(composedSchema.getAllOf().size(), 2);
+    }
+
     @Test(description = "OpenAPI v2 converter - no model in body parameter")
     public void testIssue756() throws Exception {
         OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_756_JSON);
diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-740.yaml b/modules/swagger-parser-v2-converter/src/test/resources/issue-740.yaml
new file mode 100644
index 00000000..efb60aa9
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-740.yaml
@@ -0,0 +1,66 @@
+swagger: '2.0'
+info:
+  description: 'Test'
+  version: 1.0.0
+  title: OpenAPI Test Issue 740
+  license:
+    name: Apache-2.0
+    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
+host: petstore.swagger.io
+basePath: /v2
+schemes:
+  - http
+paths:
+  /ping:
+    post:
+      summary: test
+      operationId: pingOperation
+      consumes:
+        - application/json
+      produces:
+        - application/json
+      parameters:
+        - in: body
+          name: body
+          required: true
+          schema:
+            $ref: '#/definitions/Entity'
+      responses:
+        '200':
+          description: OK
+          schema:
+            $ref: '#/definitions/Action'
+definitions:
+  Entity:
+    type: "object"
+    properties:
+      id:
+        type: "string"
+        example: "1438752"
+        description: "The ID of the entity."
+        readOnly: true
+    title: "Entity"
+    description: "A base object for all API entities."
+  Action:
+    title: "Action"
+    allOf:
+    - $ref: "#/definitions/Entity"
+    - type: "object"
+      required:
+      - "action_type"
+      discriminator: "action_type"
+      properties:
+        name:
+          type: "string"
+          description: "The name of the action."
+          readOnly: true
+        action_type:
+          $ref: "#/definitions/ActionType"
+  ActionType:
+    type: "string"
+    title: "ActionType"
+    description: "Some enum"
+    enum:
+    - "value1"
+    - "value2"
+    x-swagger-router-model: "ActionTypeDto"
\ No newline at end of file