swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[CSHARP] Duplicate constructor parameters for derived classes

Open mhallin79 opened this issue 3 years ago • 0 comments

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.

mhallin79 avatar Sep 19 '22 23:09 mhallin79