Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
kevinoid opened this issue Oct 21, 2019 · 3 comments
Open
5 of 6 tasks

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

kevinoid opened this issue Oct 21, 2019 · 3 comments

Comments

@kevinoid
Copy link
Contributor

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

@auto-labeler
Copy link

auto-labeler bot commented Oct 21, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@kevinoid
Copy link
Contributor Author

Possibly related: #3725

@jzbyers
Copy link

jzbyers commented Nov 13, 2019

@kevinoid thanks for finding this. I'm running into the same issue and plan to use the string type until the date-time struct type can be generated as a Nullable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants