swagger-api / swagger-api/swagger-codegen

[CSHARP] Duplicate constructor parameters for derived classes

Open
#11,900 0 comments 0 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

If a inherited model has properties that has a pattern defined a parameter is added twice to the derived class constructor

Swagger-codegen version

3.0.35

Swagger declaration file content or url

Simple example where both the base model Person and the inherited model PersonDetail has string properties with a pattern defined.

openapi: 3.0.0
info:
  version: '1.0'
  title: Test
components:
  schemas:
    Person:
      required:
        - name
      properties:
        name:
          type: string
        email:
          pattern: '^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$'
          type: string
    PersonDetail:
      required:
        - surname
        - ssn
      allOf:
        - $ref: '#/components/schemas/Person'
        - properties:
            surname:
              type: string        
            ssn:
              pattern: '^\d{3}-\d{2}-\d{4}$'
              type: string
            phone:
              pattern: '^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$'
              type: string
Command line used for generation

java -jar swagger-codegen-cli-3.0.35.jar generate -l csharp -i "<path-to-spec>"

Steps to reproduce
  1. Generate the C# files using the command line above
  2. Build the project
  3. There will be two build errors in \src\IO.Swagger\Model\PersonDetail.cs
    • The parameter name 'ssn' is a duplicate
    • The parameter name 'phone' is a duplicate

Note that the base class Person has no problem with the email address that also is defined with a pattern.

Actual result

Just including the constructor of the PersonDetail class. Notice the ssn and phone exist twice,

   public partial class PersonDetail : Person,  IEquatable<PersonDetail>, IValidatableObject
    {
        public PersonDetail(string surname = default(string), string ssn = default(string), string phone = default(string), string name = default(string), string email = default(string), string ssn = default(string), string phone = default(string)) : base(name, email)
        {
        }
        
Expected result

Just including the constructor of the PersonDetail class. In this example ssn and phone exist once.

   public partial class PersonDetail : Person,  IEquatable<PersonDetail>, IValidatableObject
    {
        public PersonDetail(string surname = default(string), string ssn = default(string), string phone = default(string), string name = default(string), string email = default(string)) : base(name, email)
        {
        }
               
Related issues/PRs

N/A

Suggest a fix/enhancement

Only generate parameters for properties once in the constructor for derived classes as duplicate parameters cause build errors.

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

Generate the supplied OpenAPI example with the C# generator and inspect the generated src/IO.Swagger/Model/PersonDetail.cs constructor. Compare its parameters with the base Person constructor and the expected output, then build the generated project to confirm that ssn and phone each appear only once.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.