OpenAPITools / OpenAPITools/openapi-generator

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

Open
#4,194 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Issue: Bug
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)?
  • 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

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 by generating the C# client from the OpenAPI declaration in openapi.yaml with the csharp generator command shown. Trace how optional query parameters are represented in the generated GetCount signature and request, then verify that omitted value-type parameters are not sent while explicitly supplied values still are.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.