[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
- Save OpenAPI declaration from above.
- 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