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][csharp-netcore] Duplicate methods in model class #14350

Closed
5 of 6 tasks
arvindpdmn opened this issue Jan 2, 2023 · 9 comments · Fixed by #14889 or #15377
Closed
5 of 6 tasks

[BUG][csharp-netcore] Duplicate methods in model class #14350

arvindpdmn opened this issue Jan 2, 2023 · 9 comments · Fixed by #14889 or #15377

Comments

@arvindpdmn
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)?
  • 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

When oneOf or anyOf constructs are used, the generated model file has duplicate constructor and duplicate getObject() method. There's no reference to the actual properties (aId, bId, cId, dId) in the generated file. These problems are not seen with allOf. The attached example YAML file uses oneOf. The same problem will be observed if it's replaced with anyOf.

With aspnetcore, the model file has no duplicates but shows other problems, such as no constructor. This issue focuses only on csharp-netcore.

openapi-generator version

Version used is 6.2.1. Did not test against the latest commit on the master branch.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: '1.0.0'
  title: 'Problem example with OneOf'
paths: {}
components:
  schemas:
    SomeId:
      type: object
      properties:
        aId:
          $ref: '#/components/schemas/AId'
        bId:
          $ref: '#/components/schemas/BId'
        cId:
          $ref: '#/components/schemas/CId'
        dId:
          $ref: '#/components/schemas/DId'
      oneOf:
        - required: [ aId ]
        - required: [ bId ]
        - required: [ cId ]
    AId:
      type: integer
      minimum: 0
      maximum: 10
    BId:
      type: integer
    CId:
      type: integer
    DId:
      type: integer
Generation Details
java -jar C:\openapi-generator-cli.jar generate  -i C:\ModelExample.yaml -g csharp-netcore -o C:\out --additional-properties=targetFramework=net6.0  --additional-properties=packageName=Com.Example
Steps to reproduce

Just run the command above and check the generated model file. We will see the duplicates.

Related issues/PRs

This could be related: #8382

Suggest a fix

None at the moment

@davit-asryan-sveasolar
Copy link

Having the same issue

@wing328
Copy link
Member

wing328 commented Mar 6, 2023

The duplicated methods in this case can be fixed via the REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY option in the opeanpi normalizer

e.g.

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g csharp-netcore -i /tmp/a.yaml -o /tmp/duplicated2/ --openapi-normalizer REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY=true

reference: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer

@arvindpdmn
Copy link
Author

Using openapi-generator-cli-6.5.0-20230329.141901-70.jar I tested this. It is fixed though there's no validation code to ensure anyOf or oneOf constraints are met.

In one example, there's still a problem (code does not compile): HssAuthenticationVectors in file https://forge.3gpp.org/rep/all/5G_APIs/-/blob/REL-16/TS29503_Nudm_UEAU.yaml . The generated code has three GetList() methods that differ only by the return type.

@wing328
Copy link
Member

wing328 commented Apr 1, 2023

it's fixed by #15007 instead. Please enable the option skipOneOfAnyOfGetter

@arvindpdmn
Copy link
Author

Tried with openapi-generator-cli-6.5.0-20230329.141901-70.jar few weeks back. Was able to compile the generated code.
Today tried with openapi-generator-cli-6.6.0-20230422.085013-33.jar. Compilation fails. I see duplicate methods generated. The offending definition is like this:

        subscribedDnnList:
          type: array
          items:
            anyOf:
              - $ref: 'TS29571_CommonData.yaml#/components/schemas/Dnn'
              - $ref: 'TS29571_CommonData.yaml#/components/schemas/WildcardDnn'

Generated code is:

    public partial class AccessAndMobilitySubscriptionDataSubscribedDnnListInner : AbstractOpenAPISchema, IEquatable<AccessAndMobilitySubscriptionDataSubscribedDnnListInner>, IValidatableObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AccessAndMobilitySubscriptionDataSubscribedDnnListInner" /> class
        /// with the <see cref="string" /> class
        /// </summary>
        /// <param name="actualInstance">An instance of string.</param>
        public AccessAndMobilitySubscriptionDataSubscribedDnnListInner(string actualInstance)
        {
            this.IsNullable = false;
            this.SchemaType= "anyOf";
            this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AccessAndMobilitySubscriptionDataSubscribedDnnListInner" /> class
        /// with the <see cref="string" /> class
        /// </summary>
        /// <param name="actualInstance">An instance of string.</param>
        public AccessAndMobilitySubscriptionDataSubscribedDnnListInner(string actualInstance)
        {
            this.IsNullable = false;
            this.SchemaType= "anyOf";
            this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
        }

       ...

@wing328
Copy link
Member

wing328 commented Apr 26, 2023

@arvindpdmn did you enable the option skipOneOfAnyOfGetter (not a normalizer rule) when generating the csharp-netcore client?

@wing328 wing328 modified the milestone: 6.6.0 Apr 26, 2023
@arvindpdmn
Copy link
Author

Yes. --additional-properties=targetFramework=net7.0,skipOneOfAnyOfGetter=true was the option used.

@wing328
Copy link
Member

wing328 commented Apr 26, 2023

@arvindpdmn for the following:

        subscribedDnnList:
          type: array
          items:
            anyOf:
              - $ref: 'TS29571_CommonData.yaml#/components/schemas/Dnn'
              - $ref: 'TS29571_CommonData.yaml#/components/schemas/WildcardDnn'

What's the URL to the full spec so that I can try to repeat the issue locally?

@wing328 wing328 reopened this Apr 26, 2023
@arvindpdmn
Copy link
Author

arvindpdmn commented Apr 26, 2023

This is the URL: https://forge.3gpp.org/rep/all/5G_APIs/-/blob/REL-16/TS29503_Nudm_SDM.yaml

Full command used in Gradle (I used local filename rather than the URL above):

java -jar lib/openapi-generator-cli-6.6.0-20230422.085013-33.jar generate -g csharp-netcore --package-name Com.Example --additional-properties=targetFramework=net7.0,skipOneOfAnyOfGetter=true -i ${fname} -o build/generated/Nudm_SDM --openapi-normalizer SIMPLIFY_BOOLEAN_ENUM=true,SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING=true,REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY=true,ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE=true,SIMPLIFY_ONEOF_ANYOF=true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment