Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Java] Use of anyOf leads to uncompilable output (missing class) #5938

Closed
4 of 6 tasks
ondrakucera opened this issue Apr 15, 2020 · 3 comments
Closed
4 of 6 tasks

Comments

@ondrakucera
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Use of anyOf leads to a model class referencing another model class which isn't generated. When using the definitions below, the following field is generated within Partner:

private JsonNullable<AnyOfAddress> partner2Address = JsonNullable.<AnyOfAddress>of(null);

The problem is that the class AnyOfAddress (org.openapitools.client.model.AnyOfAddress fully qualified) doesn't exist. It should probably be generated, too, but it isn't.

openapi-generator version

4.3.0

OpenAPI declaration file content or url

The example is actually split into two files because that's how the original set of files looked like (I only deleted most of the contents and all the other files that were originally there).

address.yaml:

openapi: 3.0.2
info:
    title: Address
    version: '1.0'
paths:
    '/Addresses(''{AddressID}'')':
        parameters:
            - description: 'key: AddressID'
              in: path
              name: AddressID
              required: true
              schema:
                  type: string
                  maxLength: 10
        get:
            summary: Get entity from Addresses by key
            responses:
                '200':
                    description: Retrieved entity
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Address'
components:
    schemas:
        Address:
            title: Address
            type: object
            properties:
                AddressID:
                    type: string
                    maxLength: 10
                Street:
                    type: string
                    maxLength: 60
                StreetNumber:
                    type: string
                    maxLength: 10
                City:
                    type: string
                    maxLength: 40

partner.yaml:

openapi: 3.0.2
info:
    title: Partner
    version: '1.0'
paths:
    '/Partners(''{PartnerId}'')':
        parameters:
            - description: 'key: PartnerId'
              in: path
              name: PartnerId
              required: true
              schema:
                  type: string
                  maxLength: 10
        get:
            summary: Get entity from Partners by key
            responses:
                '200':
                    description: Retrieved entity
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Partner'
components:
    schemas:
        Partner:
            title: Partner
            type: object
            properties:
                PartnerId:
                    type: string
                    maxLength: 10
                LastName:
                    type: string
                    maxLength: 40
                FirstName:
                    type: string
                    maxLength: 40
                Partner2Address:
                    anyOf:
                        - $ref: 'address.yaml#/components/schemas/Address'
                    nullable: true
Command line used for generation
-g java -p dateLibrary=java8,hideGenerationTimestamp=true,java8=true,library=resttemplate -i partner.yaml
Steps to reproduce
Related issues/PRs
Suggest a fix
@auto-labeler
Copy link

auto-labeler bot commented Apr 15, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@ondrakucera
Copy link
Author

To give more light on the example:

  • It is a (stripped down) OpenAPI definition of OData (v4) interfaces provided by SAP Netweaver Gateway. In other words, it isn't hand-written, it is generated (god knows how ...), which means it could be hard to manually modify it, unless it can be somehow done in SAP and then re-generated. (All of this is done by a third party, I'm only provided with the resulting generated JSONs.)
  • That also partially answers the question "why is there anyOf at all when there's just one item in the anyOf collection": I have no idea.
  • My suspicion is the reason is that nullable thing. If I remove the anyOf altogether (meaning that Partner2Address now contains two things: $ref and nullable), two things happen:
    • IntelliJ IDEA tells me the JSON is now invalid (IDEA does some kind of schema validation).
    • Even though it is probably invalid, the classes still can be generated by openapi-generator. However, the result is completely the same as if there was no nullable at all. In other words, Partner now contains private Address partner2Address, not private JsonNullable<Address> partner2Address.
    • So I assume that some SAP-related tool generates it this way in order to force the attribute to be nullable. Personally, I couldn't care less, I can deal with nullable data in my application logic but right now I can't generate compilable classes to be used.

@wing328
Copy link
Member

wing328 commented Nov 23, 2023

no longer an issue with the latest master

Generating /private/tmp/anyof/target/apidocs/overview-summary.html...
Generating /private/tmp/anyof/target/apidocs/help-doc.html...
[INFO] Building jar: /private/tmp/anyof/target/openapi-java-client-1.0-javadoc.jar
[INFO] 
[INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-sources) @ openapi-java-client ---
[INFO] Building jar: /private/tmp/anyof/target/openapi-java-client-1.0-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.382 s
[INFO] Finished at: 2023-11-23T19:50:02+08:00
[INFO] ------------------------------------------------------------------------

@wing328 wing328 closed this as completed Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants