OpenAPITools / OpenAPITools/openapi-generator

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

Open
#15,117 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided OpenAPI declaration and csharp-netcore generation command. Inspect the csharp-netcore generator's oneOf model and constructor handling; the fix is complete when generated SomethingMydata code provides an unambiguous way to initialize either Alpha or HelloWorld and the generated output is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.