Skip to content

[BUG] [Java] [okhttp-gson] Gson fails to serialize LocalDateTime #24642

Description

@ckoegel

#### 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Java generator maps format: date-time-local to java.time.LocalDateTime, but the okhttp-gson library's JSON.mustache never registers a Gson TypeAdapter for LocalDateTime. It only registers adapters for Date, java.sql.Date, OffsetDateTime, LocalDate, and byte[], so Gson falls back to ReflectiveTypeAdapterFactory, which calls setAccessible(true) on java.time.LocalDateTime's private fields which throws:

com.google.gson.JsonIOException: Failed making field 'java.time.LocalDateTime#date' accessible; either increase its visibility or write a custom TypeAdapter for its declaring type.
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @71aa8854

Any spec with a field using format: date-time-local creates a client that throws the first time that field is deserialized.

Relevant source:

  • AbstractJavaCodegen.java:typeMapping.put("date-time-local", "LocalDateTime"); and importMapping.put("LocalDateTime", "java.time.LocalDateTime");
  • modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache: adapter registrations in the static initializer; no LocalDateTime entry
openapi-generator version

7.24.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: date-time-local snippet
  version: 1.0.0
paths:
  /thing:
    get:
      operationId: getThing
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Thing'
components:
  schemas:
    Thing:
      type: object
      properties:
        occurredAt:
          type: string
          format: date-time-local
Generation Details
openapi-generator-cli generate -i snippet.yml -g java --library okhttp-gson -o .
Steps to reproduce
  1. Generate the client with the spec and command above.
  2. Run the following to deserialize {"occurredAt": "2025-09-29T01:23:00"} into Thing.
mvn -q -Dmaven.test.skip=true package
mvn -q dependency:build-classpath -Dmdep.outputFile=cp.txt

cat > Repro.java <<'EOF'
import org.openapitools.client.model.Thing;

public class Repro {
    public static void main(String[] args) throws Exception {
        System.out.println(Thing.fromJson("{\"occurredAt\":\"2025-09-29T01:23:00\"}"));
    }
}
EOF

java -cp "target/classes:$(cat cp.txt)" Repro.java

Actual: JsonIOException / InaccessibleObjectException as above.
Expected: deserializes into a LocalDateTime.

Related issues/PRs

#13889
#23395

Suggest a fix

Add a LocalDateTimeTypeAdapter to the okhttp-gson JSON.mustache, mirroring the existing LocalDateTypeAdapter. I have this working and will open a PR shortly.

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