OpenAPITools / OpenAPITools/openapi-generator

Derived class sets invalid default value in constructor #csharp-netcore

Open
#9,651 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ x] Have you provided a full minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [ x] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [ -] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Example with output sent in uploaded zip-file.

Using "generatorName": "csharp-netcore"
Generation results in derived class with default value for parameter 'payeeKind'

PayeeKindEnum payeeKind = "MerchantSolution"

openapi-generator version

5.0.1

OpenAPI declaration file content or url
swagger: '2.0'
info:
  title: eCommerce internal
  version: '1.0'
  description: ...
  contact:
    name: Vipps
basePath: /internal/v1
schemes:
  - https
parameters: {}
paths:
  '/transactions/{payerId}':
    parameters:
      - type: string
        name: payerId
        in: path
        required: true
      - name: Authorization
        in: header
        type: string
        description: Bearer <token issued to the identity of the client>
      - name: X-Global-Context-Id
        in: header
        type: string
        description: ...
    get:
      operationId: GetTransactionLog
      consumes:
        - application/json
      produces:
        - application/json;charset=UTF-8
      description: ...
      summary: GetTransactionLog
      parameters:
        - name: payerId
          in: path
          required: true
          description: ...
          type: string
          pattern: '^[1-9][0-9]{0,19}$'
      responses:
        '200':
          description: Transaction log
          schema:
            $ref: '#/definitions/PersonSolution'
        '400':
          description: 'Bad request, e.g., invalid parameters'
          schema:
            $ref: '#/definitions/Error'
        '401':
          description: Unauthorized
          schema:
            $ref: '#/definitions/Error'
        '404':
          description: Not found
          schema:
            $ref: '#/definitions/Error'
        '500':
          description: An unhandled error occurred
          schema:
            $ref: '#/definitions/Error'
      tags:
        - Default
  '/transactions/salesunit/{salesUnitId}':
    parameters:
      - type: string
        name: salesUnitId
        in: path
        required: true
      - name: Authorization
        in: header
        type: string
        description: Bearer <MSI token issued to the identity of the client>
      - name: X-Global-Context-Id
        in: header
        type: string
        description: ''
    get:
      operationId: GetMerchantSolution
      consumes:
        - application/json
      produces:
        - application/json;charset=UTF-8
      description: Get the transaction log for an order paid via Payments Engine.
      summary: GetMerchantSolution
      parameters:
        - name: salesUnitId
          in: path
          required: true
          description: Sales unit Id
          type: string
          pattern: '^[1-9][0-9]{0,19}$'
      responses:
        '200':
          description: Transaction log
          schema:
            $ref: '#/definitions/MerchantSolution'
        '400':
          description: 'Bad request, e.g., invalid parameters'
          schema:
            $ref: '#/definitions/Error'
        '401':
          description: Unauthorized
          schema:
            $ref: '#/definitions/Error'
        '404':
          description: Not found
          schema:
            $ref: '#/definitions/Error'
        '500':
          description: An unhandled error occurred
          schema:
            $ref: '#/definitions/Error'
      tags:
        - Default
definitions:
  SolutionTag:
    type: string
    description: ''
    enum:
      - shoppingbasket
      - p2p_request
  Solution:
    type: object
    discriminator: payeeKind
    description: ''
    required:
      - payeeKind
      - tags
    properties:
      payeeKind:
        type: string
        description: ''
        enum:
          - person
          - saleunit
      tags:
        type: array
        items:
          $ref: '#/definitions/SolutionTag'
  MerchantSolution:
    x-class: saleunit
    allOf:
      - $ref: '#/definitions/Solution'
      - type: object
        required:
          - payerPresent
        properties:
          payerPresent:
            type: boolean
            description: ''
          partnerID:
            type: string
            description: ''
  PersonSolution:
    x-class: person
    allOf:
      - $ref: '#/definitions/Solution'
      - type: object
  Error:
    type: object
    properties:
      message:
        type: string
        description: |
          Description of the error.
host: test
tags:
  - name: Default

Generation Details

None

Steps to reproduce

Just run
openapi-generator-cli generate

Related issues/PRs
Suggest a fix

I don't understand why a constructor with all members as parameters with default values is generated for a DTO.
A public parameterless constructor is good enough.
Eg. removing this constucor in genericModel.mustache will fix the problem.
I don't think there are a way of setting default values for a $ref type. If there is this value should be used.
I my case i enum get at string as default which is not supportet i c#

TestClient.zip

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 csharp-netcore generator and the genericModel.mustache template mentioned in the report. Run generation with the supplied OpenAPI declaration, then inspect the derived DTO constructor and enum default value. Done means the generated C# code no longer assigns the invalid string default to the enum reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
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.