OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] Error reading `oneOf` values

Open
#13,963 0 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

The autogenerated code for the oneOf (the keyword from the OAS3 specification) does not work for primitives values (of type number, string, boolean, and possibly more). I believe that there are two methods involved and that they are written faulty.

Methods:

  1. ReadJson
  2. FromJson
openapi-generator version

Version 6.2.1.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
    title: "OneOf Test"
    version: 1.0.0
paths:
    "/v1/oneOf/test":
        get:
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: "#/components/schemas/oneOfSchema"
                "404":
                    description: "Not Found"
components:
    schemas:
        oneOfSchema:
            title: OneOf
            properties:
                key:
                    type: string
                value:
                    oneOf:
                        - type: number
                        - type: string
                        - type: boolean
            type: object
Generation Details

npx @openapitools/openapi-generator-cli generate -i spec.yml -g csharp-netcore.

Steps to reproduce

Navigate to https://github.com/TimurBas/OpenAPIGeneratorBugJObjectOneOf under the branch error and read the README.

Related issues/PRs

I don't think so.

Suggest a fix

Navigate to https://github.com/TimurBas/OpenAPIGeneratorBugJObjectOneOf under the master branch.
Note that this only fixes it in my project and not in the generator itself (don't know where to look for tbh).

Explanation of the fix(es):

  1. ReadJson
    • This part return SomeValue.FromJson(JObject.Load(reader).ToString(Formatting.None)); expects a JObject but as alluded earlier oneOf values can be primitives and therefore the loading from the reader fails since it is not a JObject but e.g. a string. Thus, the loading should be more generic. I'm not too familiar with the Newtonsoft.Json package but writing JToken instead of JObject did the work for me.
  2. FromJson
    • Assumption: The ReadJson part has changed to what was suggested. In each part where deserialization happens then for some reason, it successfully deserializes e.g. the number 2 to a bool, string which is obviously wrong, and of course a decimal. I fixed it by using the native System.Text.Json.JsonSerializer instead because it does not successfully deserialize it into a bool or string which is what we would normally expect. It seems that Newtonsoft's serializer is not as strict as the native serializer which is why I used the native one instead. However, there might be a workaround for not using the native one but I haven't had time to spend looking at that.

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

Reproduce the issue with the supplied OpenAPI declaration using the csharp-netcore generator command, then inspect the generated ReadJson and FromJson methods. Confirm that oneOf values of number, string, and boolean types are read correctly without requiring a JObject, and verify the generated code against the example repository's README.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.