question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][Java] OneOf-Class not generated

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The OpenAPI Generator v5.3.1 & 5.3.0 does not generate a OneOf-Model-Class which results in the code not being able to be built. I found the exact same issue in Java jersey2, Java native (and Rust reqwest). This confuses me as the Java generators are supposed to support OneOf (https://github.com/OpenAPITools/openapi-generator/pull/10463#discussion_r717187400 and https://github.com/OpenAPITools/openapi-generator/pull/7263). I expected the Generator to support OneOf in Java as these two links say.

openapi-generator version

5.3.1 & 5.3.0

OpenAPI declaration file content or url

https://raw.githubusercontent.com/jdegre/5GC_APIs/master/TS29510_Nnrf_NFManagement.yaml

Generation Details

(Jersey2) java -jar openapi-generator-cli-5.3.1-20211116.151401-16.jar generate -i Desktop/5GC_APIs/TS29510_Nnrf_NFManagement.yaml -g java -o Desktop/NRFManagement_java_jersey2 --library jersey2 --additional-properties=packageName=nfmanagement

OR

(Native) java -jar openapi-generator-cli-5.3.1-20211116.151401-16.jar generate -i Desktop/5GC_APIs/TS29510_Nnrf_NFManagement.yaml -g java -o Desktop/NRFManagement_java_native --library native --additional-properties=packageName=nfmanagement

Steps to reproduce
  1. Generate Code of NFManagement with OpenAPI Generator (with Generation Details above)
  2. Try to build it
  3. Search for Error “Cannot resolve symbol ‘OneOfNfInstanceIdCondNfInstanceIdListCondNfTypeCondServiceNameCondAmfCondGuamiListCondNetworkSliceCondNfGroupCondNfSetCondNfServiceSetCondUpfCondScpDomainCondNwdafCondNefCondDccfCond’”
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/9791 https://github.com/OpenAPITools/openapi-generator/issues/10514 https://github.com/OpenAPITools/openapi-generator/issues/9981

Suggest a fix

The error is caused by “TS29510_Nnrf_NFManagement.yaml” line 1449-1465. Might be worth looking into.

subscrCond:
          oneOf:
            - $ref: '#/components/schemas/NfInstanceIdCond'
            - $ref: '#/components/schemas/NfInstanceIdListCond'
            - $ref: '#/components/schemas/NfTypeCond'
            - $ref: '#/components/schemas/ServiceNameCond'
            - $ref: '#/components/schemas/AmfCond'
            - $ref: '#/components/schemas/GuamiListCond'
            - $ref: '#/components/schemas/NetworkSliceCond'
            - $ref: '#/components/schemas/NfGroupCond'
            - $ref: '#/components/schemas/NfSetCond'
            - $ref: '#/components/schemas/NfServiceSetCond'
            - $ref: '#/components/schemas/UpfCond'
            - $ref: '#/components/schemas/ScpDomainCond'
            - $ref: '#/components/schemas/NwdafCond'
            - $ref: '#/components/schemas/NefCond'
            - $ref: '#/components/schemas/DccfCond'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
lparcommented, Dec 15, 2021

It looks as if there’s a problem with oneOf support for properties; as the summary indicates, the generator fails to generate the appropriate wrapper class to wrap the individual data classes.

A workaround seems to be to rewrite the spec file to introduce an explicit wrapper class. The class can use oneOf successfully.

For example, we had:

    CredentialOptions:
      type: object
      properties:
        secret:
          type: object
          additionalProperties: true
          oneOf:
            - $ref: '#/components/schemas/MiradoreCredentialFields'
            - $ref: '#/components/schemas/AWSCredentialFields'
            - $ref: '#/components/schemas/AzureClientSecretCredentialFields'
            - $ref: '#/components/schemas/AzureUsernamePasswordCredentialFields'
            - $ref: '#/components/schemas/CrowdstrikeCredentialFields'
            - $ref: '#/components/schemas/CensysCredentialFields'
            - $ref: '#/components/schemas/SNMPv2CommunitiesCredentialFields'
            - $ref: '#/components/schemas/SNMPv3CredentialFields'
            - $ref: '#/components/schemas/VMwareCredentialFields'
        [other properties omitted]

This caused code to be generated with a missing class with an extremely long name starting OneOf. I added an explicit wrapper:

    CredentialFields:
      oneOf:
        - $ref: '#/components/schemas/MiradoreCredentialFields'
        - $ref: '#/components/schemas/AWSCredentialFields'
        - $ref: '#/components/schemas/AzureClientSecretCredentialFields'
        - $ref: '#/components/schemas/AzureUsernamePasswordCredentialFields'
        - $ref: '#/components/schemas/CrowdstrikeCredentialFields'
        - $ref: '#/components/schemas/CensysCredentialFields'
        - $ref: '#/components/schemas/SNMPv2CommunitiesCredentialFields'
        - $ref: '#/components/schemas/SNMPv3CredentialFields'
        - $ref: '#/components/schemas/VMwareCredentialFields'

    CredentialOptions:
      type: object
      properties:
        secret:
          $ref: '#/components/schemas/CredentialFields'
        [other properties omitted]

…and the generated code (with --library jersey2) then includes an implementation of CredentialFields which tries deserializing to the alternatives in turn. Everything compiles.

0reactions
llakhchcommented, Dec 6, 2022

OneOf-Model-Classes are generating without any issue with openapi-generator-cli-6.2.1. It seems this issue has been resolved now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI models not being generated when using oneOf
I have oneOf generation working for java using openapi-generator 5.4.0 but I do add a discriminator field (like requestType: type: string enum: ...
Read more >
Language Guide | Protocol Buffers - Google Developers
For Java, the compiler generates a .java file with a class for each message type, ... Moving any fields into an existing oneof...
Read more >
Documentation for the spring Generator
Generates a Java SpringBoot Server application using the SpringDoc integration. #CONFIG OPTIONS. These options may be applied as additional- ...
Read more >
Inheritance and Polymorphism - Swagger
In OpenAPI 3.0, to describe such a model, you can use the oneOf or anyOf keywords: ... If the property values do not...
Read more >
Open API Server Implementation Using OpenAPI Generator
Learn how to generate a Spring-based server stub from a YAML ... Notably, there's support for C++, C#, Java, PHP, Python, Ruby, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found