OpenAPITools / OpenAPITools/openapi-generator
[BUG] [C#] model-name-prefix combined with polymorhphism & discriminator generates code that does not compile
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?
- 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?
Description
openapi-generator version
7.15.0
OpenAPI declaration file content or url
I have isolated just the one endpoint w/ request parameters that appears to reproduce this bug. It was encountered in the Ory project (https://github.com/ory/sdk/issues/434).
Generation Details
$ OPENAPI_GENERATOR_CLI_SEARCH_URL=DEFAULT npx @openapitools/openapi-generator-cli@2.23.1 version-manager set 7.15.0
$ npx @openapitools/openapi-generator-cli@2.23.1 generate -i mre.json -g csharp --model-name-prefix="Client"
Steps to reproduce
Run CLI commands above, pointing at mre.json
Open ClientUpdateRecoveryFlowBody.json
Observe invalid C# in the Read override method for the System.Text.Json.JsonConverter:
public override ClientUpdateRecoveryFlowBody Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert, JsonSerializerOptions jsonSerializerOptions)
{
int currentDepth = utf8JsonReader.CurrentDepth;
if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
JsonTokenType startingTokenType = utf8JsonReader.TokenType;
Option<ClientUpdateRecoveryFlowBody.MethodEnum?> method = default;
Utf8JsonReader utf8JsonReaderDiscriminator = utf8JsonReader;
while (utf8JsonReaderDiscriminator.Read())
{
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReaderDiscriminator.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReaderDiscriminator.CurrentDepth)
break;
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReaderDiscriminator.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReaderDiscriminator.CurrentDepth)
break;
if (utf8JsonReaderDiscriminator.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReaderDiscriminator.CurrentDepth - 1)
{
string? localVarJsonPropertyName = utf8JsonReaderDiscriminator.GetString();
utf8JsonReaderDiscriminator.Read();
if (localVarJsonPropertyName?.Equals("method") ?? false)
{
string? discriminator = utf8JsonReaderDiscriminator.GetString();
if (discriminator?.Equals("code") ?? false)
{
Utf8JsonReader utf8JsonReader = utf8JsonReader;
= JsonSerializer.Deserialize<>(ref utf8JsonReader, jsonSerializerOptions); // fails to compile
}
if (discriminator?.Equals("link") ?? false)
{
Utf8JsonReader utf8JsonReader = utf8JsonReader;
= JsonSerializer.Deserialize<>(ref utf8JsonReader, jsonSerializerOptions); // fails to compile
}
}
}
}
// ...
If --model-name-prefix not specified, it works correctly.
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
Start by running the documented openapi-generator CLI command with mre.json and --model-name-prefix="Client". Inspect the generated ClientUpdateRecoveryFlowBody.json and the C# generator path handling polymorphism, discriminators, and model-name prefixes; done means the generated C# compiles, while generation without the prefix continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100