OpenAPITools / OpenAPITools/openapi-generator
[BUG][C#] Error reading `oneOf` values
Nobody has claimed this yet.
- 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:
ReadJsonFromJson
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):
ReadJson- This part
return SomeValue.FromJson(JObject.Load(reader).ToString(Formatting.None));expects aJObjectbut as alluded earlieroneOfvalues can be primitives and therefore the loading from the reader fails since it is not aJObjectbut e.g. astring. Thus, the loading should be more generic. I'm not too familiar with theNewtonsoft.Jsonpackage but writingJTokeninstead ofJObjectdid the work for me.
- This part
FromJson- Assumption: The
ReadJsonpart has changed to what was suggested. In each part where deserialization happens then for some reason, it successfully deserializes e.g. the number2to abool,stringwhich is obviously wrong, and of course adecimal. I fixed it by using the nativeSystem.Text.Json.JsonSerializerinstead because it does not successfully deserialize it into aboolorstringwhich is what we would normally expect. It seems thatNewtonsoft'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.
- Assumption: The
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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