Skip to content

[BUG][csharp][generichost] useDateTimeOffset loses the original wire offset during deserialization #23338

@sasha-borodin

Description

@sasha-borodin

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output? (see below)
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a C# client with the generichost templates and useDateTimeOffset=true, string/date-time properties are emitted as DateTimeOffset model properties, but the generated JSON converter first deserializes the incoming value into DateTime. That drops the original offset and replaces it with the local system offset when the DateTimeOffset is constructed.

openapi-generator version

7.21.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: DateTimeOffset Repro
  version: 1.0.0
paths:
  /example:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Example"
components:
  schemas:
    Example:
      type: object
      properties:
        timestamp:
          type: string
          format: date-time
      required:
        - timestamp
Generation Details
  • Generator: OpenAPI Generator C# client
  • Library: generichost
  • Config: useDateTimeOffset=true
Steps to reproduce

Generation command:

openapi-generator-cli generate \
  -g csharp \
  -i openapi.yaml \
  -o out \
  --additional-properties=library=generichost,useDateTimeOffset=true

Repro input:

{ "timestamp": "2026-03-24T10:15:30+02:00" }

Observed result:
The generated model property is DateTimeOffset, but the deserialization path first parses the value as DateTime, so the offset from the JSON payload is not preserved.

Related issues/PRs

none found

Suggest a fix

Update the generichost date-time deserialization path to deserialize directly into DateTimeOffset when useDateTimeOffset=true, or otherwise preserve the original offset information all the way through the converter.

We customized the JsonConverter.mustache template, line 224, by inserting {{#useDateTimeOffset}}Offset{{/useDateTimeOffset}}:

{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}} = {{>OptionProperty}}JsonSerializer.Deserialize<DateTime{{#useDateTimeOffset}}Offset{{/useDateTimeOffset}}{{#isNullable}}?{{/isNullable}}>(ref utf8JsonReader, jsonSerializerOptions));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions