swagger-api / swagger-api/swagger-codegen

[csharp] Mustache templates do not distinguish generated and native parent classes

Open
#7,669 8 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The mustache templates do not distinguish generated and native parent classes resulting in invalid code. For example, the override modifier is set in the modelGeneric.mustach template for the ToJson method if a non-array parent exists. But the parent can be a dictionary as well (see Swagger declaration below).

public {{#parent}}{{^isArrayModel}}override {{/isArrayModel}}{{/parent}}{{^parent}}{{#discriminator}}virtual {{/discriminator}}{{/parent}}string ToJson()

Same for the validate method:

{{#parent}}
{{^isArrayModel}}
foreach(var x in BaseValidate(validationContext)) yield return x;
{{/isArrayModel}}
{{/parent}}

BaseValidate does not exist in the dictionary class.

Swagger-codegen version

Version: 2.3.0
Is it a regression? Yes, works well with version 2.3.0-20170713.173236-24.

Swagger declaration file content or url
"KeyValues": {
  "type": "object",
    "additionalProperties": {
      "type": "string"
    }
}

Generates:

public partial class KeyValues : Dictionary<String, string>,  IEquatable<KeyValues>, IValidatableObject
Command line used for generation
java -jar .\swagger-codegen-cli-2.3.0.jar generate -l csharp -i .\swagger.json -c .\config.json -o .\output\ -t .\templates\ --ignore-file-override .swagger-codegen-ignore
Steps to reproduce
  1. Create Swagger declaration with the definition mentioned above
  2. Create the code java -jar .\swagger-codegen-cli-2.3.0.jar generate -l csharp -i .\swagger.json
  3. Try to build the generated code. You will get an compiler error CS0115:
'KeyValues.ToJson()' : no suitable method found to override
Suggest a fix/enhancement

If there would be a template variable like isArrayModel but for dictionaries or native parent classes in general, we could adjust the existing templates easily.

For example like this:

{{#parent}}
{{^isArrayModel}}
{{^isDictionary}}
foreach(var x in BaseValidate(validationContext)) yield return x;
{{/isDictionary}}
{{/isArrayModel}}
{{/parent}}

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 with the C# template modelGeneric.mustach and trace how parent and isArrayModel are provided for dictionary models. Reproduce the KeyValues declaration from the issue, generate the C# code, and compile it to confirm the CS0115 error. Done means dictionary models no longer emit invalid override or BaseValidate code.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.