OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.8k stars 6.58k forks source link

[BUG] [csharp-netcore] Inadequate constructor with oneOf constraint #15117

Open arvindpdmn opened 1 year ago

arvindpdmn commented 1 year ago

Bug Report Checklist

Description

When oneOf is used along with a specific set of codegen options, we see this problem. In the following example, SomethingMydata.cs has two constructors: SomethingMydata() and SomethingMydata(string actualInstance). When the latter is used, it's impossible to know if the input is "Alpha" or "HelloWorld". If application code wants to initialize Something with "Alpha" there's no proper way to do this. Note the "Alpha" can contain two properties: "abc" and "def". Thus, Alpha can't be reduced to a single string.

openapi-generator version

openapi-generator-cli-6.5.0-20230329.141901-70.jar from https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.5.0-SNAPSHOT/

Did not test with 6.5.0 which has just been released. However, I expect the same problem.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: '1.0.0'
  title: 'oneOf constructor issue'
paths: {}
components:
  schemas:
    Something:
      type: object
      properties:
        mydata:
          oneOf:
            - $ref: '#/components/schemas/Alpha'
            - $ref: '#/components/schemas/HelloWorld'
      required:
        - mydata
    Alpha:
      type: object
      required:
        - abc
      properties:
        abc:
          $ref: '#/components/schemas/Abc'
        def:
          $ref: '#/components/schemas/Def'
    HelloWorld:
      type: string
      format: base64
      nullable: true
    Abc:
      type: string
      pattern: '^[A-Fa-f0-9]{32}$'
    Def:
      type: string
      pattern: '^[A-Fa-f0-9]{8,32}$'
Generation Details
java -jar lib/openapi-generator-cli-6.5.0-20230329.141901-70.jar generate -i OneOf.yaml -g csharp-netcore -o OneOf --additional-properties=targetFramework=net7.0,skipOneOfAnyOfGetter=true --additional-properties=packageName=Com.Example --skip-validate-spec --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
Steps to reproduce

As above

Related issues/PRs
  1. https://github.com/OpenAPITools/openapi-generator/issues/14350
  2. https://github.com/OpenAPITools/openapi-generator/issues/2791
Suggest a fix

No idea at the moment.

devhl-labs commented 1 year ago

Consider trying this with the generichost library.

prajon84 commented 9 months ago

👋 Any update on the above oneOf issue so far? Since csharp-netcore is merged with csharp. Do we have any update with -g csharp and --library httpclient ?