OpenAPITools / OpenAPITools/openapi-generator

[BUG] openapi-generator-cli generates C# code that does not use correct enum variants in requests

Open
#10,904 3 comments 4 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

C# enums are not correctly converted to their string representations. Take this generated enum:

/// <summary>
/// Type of compliance job to list.
/// </summary>
/// <value>Type of compliance job to list.</value>
[JsonConverter(typeof(StringEnumConverter))]
public enum ComplianceJobType
{
    /// <summary>
    /// Enum Tweets for value: tweets
    /// </summary>
    [EnumMember(Value = "tweets")]
    Tweets = 1,

    /// <summary>
    /// Enum Users for value: users
    /// </summary>
    [EnumMember(Value = "users")]
    Users = 2
}

Such a type is put incorrectly into the query params:

localVarRequestOptions.QueryParameters.Add(SpartaModels.Twitter.Client.ClientUtils.ParameterToMultiMap("", "type", type));

This code uses the variant's name (Tweets) and not the value (tweets).

openapi-generator version

5.3.1

OpenAPI declaration file content or url

https://api.twitter.com/2/openapi.json

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://api.twitter.com/2/openapi.json \
    -g csharp-netcore \
    -o /local/out/C#
  • some syntax error fixes
Steps to reproduce
  • Generate source code
  • Fix syntax errors introduced by #10897
  • See/Verify that the source code does not stringify enums correctly
Suggest a fix

You could do this: https://stackoverflow.com/a/2650090/1569755
Or just use extension methods to add a custom ToOpenAPIString method to your enums

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 with the Twitter OpenAPI declaration and the csharp-netcore generator command, then trace how query parameters reach ClientUtils.ParameterToMultiMap. Done means generated requests serialize enum members as their EnumMember string values, such as tweets, rather than names such as Tweets.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
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.