OpenAPITools / OpenAPITools/openapi-generator
[BUG][C#] Project with some inheritance fails to 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 (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?
Description
I have a C# project where I use swagger to generate the swagger json file, then I use the OpenApi generator CLI to generate the C# api project.
The problem I am encountering is that the generated C# api project does not compile.
I have recreated a minimal project that mimics my actual project, I have these two classes :
public abstract class Animal
{
protected Animal(string sound)
{
Sound = sound;
}
public virtual string Sound { get; }
public abstract string TypeName { get; }
}
public class Duck : Animal
{
public Duck(string sound) : base(sound)
{
}
public override string TypeName { get; } = nameof(Duck);
}
And, once I have generated the csharp api with openapigenerator, I get this :
namespace DuckClient.Model
{
/// <summary>
/// Animal
/// </summary>
[DataContract(Name = "Animal")]
[JsonConverter(typeof(JsonSubtypes), "TypeName")]
[JsonSubtypes.KnownSubType(typeof(Duck), "Duck")]
public partial class Animal : IValidatableObject
{
/// <summary>
/// Initializes a new instance of the <see cref="Animal" /> class.
/// </summary>
[JsonConstructorAttribute]
public Animal()
{
}
// rest of code that does not matter for this bug report
}
}
namespace DuckClient.Model
{
/// <summary>
/// Duck
/// </summary>
[DataContract(Name = "Duck")]
[JsonConverter(typeof(JsonSubtypes), "TypeName")]
public partial class Duck : Animal, IValidatableObject
{
/// <summary>
/// Initializes a new instance of the <see cref="Duck" /> class.
/// </summary>
[JsonConstructorAttribute]
public Duck() : base(sound)
{
}
// rest of code that does not matter for this bug report
}
}
As you can see, the previous code would not compile, Duck tries to call a base class's constructor that does not exist, with a parameter (sound) that does not exist either.
openapi-generator version
7.3.0
Steps to reproduce
I have created a project that recreates the issue :
- Download my test project.
- Compile it (this will generate the swagger json file in WebApplication1/Swagger/).
- Launch the generate-api.sh script (this will generate the C# api project).
- Open that project (in api/ folder).
- Compile this project (it fails).
Alternatively you can just get the WebApplication1/Swagger/public-api.json file and generate the C# api project from that.
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 linked DuckClient reproduction: compile the project, run generate-api.sh, and inspect the generated Animal and Duck model classes. Use WebApplication1/Swagger/public-api.json as the alternate input. Done means the generated C# API project compiles without the invalid base-constructor call or undefined sound parameter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java, openapi
- Domain
- api, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100