Skip to content

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

Open
@jeff9finger

Description

@jeff9finger
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 Adds the missing install-ivy build script #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

#2449
#4085
OAI/OpenAPI-Specification#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;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions