RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

Apply ParameterDateFormat to date in header as well #4760

Open Eruzo opened 6 months ago

Eruzo commented 6 months ago

Given the following parameter:

ReferenceDate:
  description: |
    A query date provided by the calling system. If non supplied the current system date will be used
  name: X-Reference-Date
  in: header
  schema:
    type: string
    format: date
  example: "2022-04-23"

The generated code would be:

if (x_Reference_Date != null)
    request_.Headers.TryAddWithoutValidation("X-Reference-Date", ConvertToString(x_Reference_Date, System.Globalization.CultureInfo.InvariantCulture));

In this case the date is passed as a full date including the timestamp in the US format. This causes the request to be rejected.

The pull requests adds a check for IsDate in the same way it exists for query parameters. In addition a check for IsSystemNullable is added. The type for the parameter generated in the function call is System.DateTimeOffset? x_Reference_Date. Without the check the generated code does not generate the ?.ToString( with the null-conditional oprator causing a compile error.

Technically this would be a breaking I guess?