OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP] When Field Names are configured to be anything other than PascalCase, the JsonConverter is not updated.

Open
#21,675 2 comments 1 reaction 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
  • [ No] Have you provided a full/minimal spec to reproduce the issue?
  • [Yes] Have you validated the input using an OpenAPI validator?
  • [Yes] Have you tested with the latest master to confirm the issue still exists?
  • [Yes] Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

I use the following command to generate:

openapi-generator-cli generate -g csharp -i openapi.yaml --additional-properties="netCoreProjectFile=true,useDateTimeOffset=true,nullableReferenceTypes=true,modelPropertyNaming=camelCase" -p "packageName=MyClient" -o client`

I get a class like:

public class SomethingJsonConverter : JsonConverter<Something>
{
  ...
  public override Something Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert, JsonSerializerOptions jsonSerializerOptions)
{
    ...
    Option<int?> id = default;

    ...
            switch (localVarJsonPropertyName)
            {
                case "Id":
                    id = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
                    break;
                default:
                    break;
            }
        }
    }
}
openapi-generator version

7.14.0

OpenAPI declaration file content or url

This is not particularly relevant to the issue. This should happen for any openAPI declaration.

Generation Details

Please see above

Steps to reproduce

Please see above

Related issues/PRs

https://github.com/openapitools/openapi-generator/issues/21543
This issue is related. I have the same problem as the OP, but I COULD get around it by using the JsonConverter IF the JsonCoverter fields were the right case or the comparison was done in case-insensitive manner.

Suggest a fix

When the JsonConverter object is generated, it obviously knows about the names of the fields, because the class has members with the field names in whatever format I specified. The problem is that the setting is not honoured in the case statement.

Alternatively, can we just get rid of the Option<string?> thing and replace it with string?

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 shown openapi-generator-cli command and inspect the generated SomethingJsonConverter. Trace how modelPropertyNaming=camelCase is applied to model members versus the converter's switch cases; done means the converter recognizes the configured field names, with regression coverage for the generated C# output.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.