Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #4194
Closed
Open
Issue created Oct 21, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][C#] Optional parameters incorrectly send default value

Created by: kevinoid

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The C# client code which is generated for an operation with optional parameters of value types causes the parameters to be sent with default values if not specified by the user, instead of not sending the parameter if not specified. This is surprising and can prevent API use (if there is no value for the parameter which behaves the same as not specifying the parameter at all).

openapi-generator version

4.1.3 I am unsure if it is a regression.

OpenAPI declaration file content or url
(Click to expand) OpenAPI 3 example with optional parameters of struct type.
openapi: '3.0.2'
info:
  title: Example API
  description: API with non-required, no-default parameters
  version: '1.0.0'
servers:
- url: https://example.com/api
components:
  schemas:
    ChangeType:
      type: string
      enum:
      - Create
      - Delete
      - Insert
      - Update

paths:
  /count:
    get:
      operationId: getCount
      parameters:
      - name: type
        in: query
        description: Only count changes of a given type
        schema:
          $ref: '#/components/schemas/ChangeType'
      - name: after
        in: query
        description: Only count changes after a given date/time
        schema:
          type: string
          format: date-time
      - name: before
        in: query
        description: Only count changes before a given date/time
        schema:
          type: string
          format: date-time
      - name: errors
        in: query
        description: Include errors in count?
        schema:
          type: boolean
      - name: limit
        in: query
        description: Maximum count
        schema:
          type: integer
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                type: integer
Command line used for generation
openapi-generator generate -g csharp -i openapi.yaml -o openapi-generator
Steps to reproduce
  1. Save OpenAPI declaration from above.
  2. Generate C# client using above command.

Observe the signature

public int GetCount (ChangeType type = default(ChangeType), DateTime after = default(DateTime), DateTime before = default(DateTime), bool errors = default(bool), int limit = default(int))`

which will add all of the query parameters to the request with (C#) default values, which is not intended by the user.

Suggest a fix

I believe struct method argument types should be Nullable<T> when not required: true, as done by Autorest. For reference, the signature generated by Autorest for the same OpenAPI doc is:

public static int? GetCount(this IExampleAPI operations, string type = default(string), System.DateTime? after = default(System.DateTime?), System.DateTime? before = default(System.DateTime?), bool? errors = default(bool?), int? limit = default(int?))

Thanks for considering, Kevin

Assignee
Assign to
Time tracking