swagger-api / swagger-api/swagger-codegen

[java] discriminator not defined when inside an allOf: inline object

Open
#4,226 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature: Composition / Inheritance Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When a discriminator is defined in a spec in an allOf inline object, it is ignored. This prevents the inheritance in the generated code from being generated correctly.

I have been studying the open api spec and swagger-codegen projects for the constraints on allOf:

It appears that swagger-codegen (language is java) only recognizes the last inline element of an allOf: array. I have not found a statement in the open api spec or code in swagger-codegen that specifies this constraint.

  1. How are inline objects supposed to be handled when there are multiple inline objects defined within an allOf array?

  2. How are discriminators inside the objects from #1 supposed to be represented in the generated code (java in this case)?

Currently, it appears that only the last inline object is recognized.

FYI: This came up in trying to define a discriminator for the inline object.

Swagger-codegen version

2.2.2-SNAPSHOT + pr 4085

Swagger declaration file content or url
definitions:

  Object:
    allOf:
      - $ref: '#/definitions/SomeOtherObject'
      - type: object
        discriminator: object_type0
        required:
          - object_type0
        properties:          
          object_type0:
            type: string
          name:
            type: string
      - type: object
        discriminator: object_type1
        required:
          - object_type1
        properties:          
          object_type1:
            type: string
          name:
            type: string
Command line used for generation

Maven

                    <plugin>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-codegen-maven-plugin</artifactId>
                        <version>${swagger-codegen.version}</version>
                        <executions>
                            <execution>
                                <id>xxx</id>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <phase>generate-test-sources</phase>
                                <configuration>
                                    <verbose>false</verbose>
                                    <environmentVariables>
                                    </environmentVariables>
                                    <inputSpec>${project.build.directory}/${api.spec.file}</inputSpec>
                                    <output>${project.build.directory}/generated/java-client</output>
                                    <language>java</language>
                                    <library>jersey2</library>
                                    <configOptions>
                                        <sourceFolder>.</sourceFolder>
                                        <dateLibrary>java8</dateLibrary>
                                        <hideGenerationTimestamp>false</hideGenerationTimestamp>
                                    </configOptions>
                                    <addCompileSourceRoot>true</addCompileSourceRoot>
                                    <bigDecimalAsString>true</bigDecimalAsString>
                                    <generateApiTests>true</generateApiTests>
                                    <generateModelTests>true</generateModelTests>
                                    <modelPackage>com.acme.api.model</modelPackage>
                                    <apiPackage>com.acme.api</apiPackage>
                                    <invokerPackage>com.acme.api.client</invokerPackage>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
Related issues

https://github.com/swagger-api/swagger-codegen/issues/2449
https://github.com/swagger-api/swagger-codegen/pull/4085
https://github.com/OAI/OpenAPI-Specification/issues/848

Suggest a Fix

Adding the following to DefaultCodeGen seems to fix the issue for me. However, this assumes that allOf: can have no more than one inline element that contains a discriminator. (My experience also shows that allOf: understands no more than one inline elements. If more than one exists, only last one is recognized).

@@ -1240,6 +1251,12 @@ public class DefaultCodegen {
                 allProperties = new LinkedHashMap<String, Property>();
                 allRequired = new ArrayList<String>();
                 m.allVars = new ArrayList<CodegenProperty>();
+                for (Model innerModel: composed.getAllOf()) {
+                    if (innerModel instanceof ModelImpl) {
+                        m.discriminator = ((ModelImpl) innerModel).getDiscriminator();
+                        break; // only one discriminator allowed at the moment.
+                    }
+                }
             } else {
                 allProperties = null;
                 allRequired = null;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultCodegen and the supplied allOf YAML, then review related issue 2449, pull request 4085, and OpenAPI issue 848 to determine the intended handling of multiple inline objects and discriminators. Verify the generated Java model inheritance and discriminator behavior against the reported case; done means the behavior is defined and the generated code is correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.