swagger-api / swagger-api/swagger-codegen
[csharp] Mustache templates do not distinguish generated and native parent classes
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
- Create Swagger declaration with the definition mentioned above
- Create the code
java -jar .\swagger-codegen-cli-2.3.0.jar generate -l csharp -i .\swagger.json - 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
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 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