Skip to content

[BUG]: Java with --datetime-provider legacy reads an unparseable date as null instead of rejecting it #3549

Open

Description

@EloB

Issue Type

quicktype output (the generated Java code accepts invalid input).

Context (Environment, Version, Language)

Input Format: JSON Schema
Output Language: Java (--datetime-provider legacy)

CLI, npm, or app.quicktype.io: CLI, built from master
Version: 24.0.0 (master at c39af7d)

Description

Found while porting quicktype to Rust (quickertype). With the legacy
date-time provider, a date, time or date-time property whose value
matches none of the formats is read as null, and the parse succeeds.
The default java8 provider throws instead.

The repository's own expected-failure sample
test/inputs/schema/optional-date-time.1.fail.date-time.json shows it:
the schema-java-datetime-legacy fixture accepts it. CI's Java job runs
java,schema-java, which does not use the legacy provider, so it does not
show up there.

Input Data

test/inputs/schema/optional-date-time.schema with
test/inputs/schema/optional-date-time.1.fail.date-time.json:

{
    "required-date": "2024-01-02",
    "required-time": "12:34:56Z",
    "required-date-time": "not a valid date-time at all",
    "optional-date": "2024-01-02",
    "optional-time": "12:34:56Z",
    "optional-date-time": "2024-01-02T12:34:56Z"
}

Expected Behaviour / Output

The input is rejected (an exception, exit status 1), as with the default
java8 provider.

Current Behaviour / Output

The fixture's App (Converter.toJsonString(Converter.fromJsonString(input)))
exits with status 0 and prints the invalid value as null:

Expected failure on input test/inputs/schema/optional-date-time.1.fail.date-time.json
  "result": "{\"optional-date\":\"2024-01-02\",\"optional-date-time\":\"2024-01-02T12:34:56.000Z\",\"optional-time\":\"12:34:56Z\",\"required-date\":\"2024-01-02\",\"required-date-time\":null,\"required-time\":\"12:34:56Z\"}",

Steps to Reproduce

  1. QUICKTEST=true FIXTURE=schema-java-datetime-legacy npm run test:fixtures -- test/inputs/schema/optional-date-time.schema
  2. Or by hand: node dist/index.js --src-lang schema --lang java --datetime-provider legacy --package io.quicktype --top-level TopLevel --src test/inputs/schema/optional-date-time.schema -o test/fixtures/java/src/main/java/io/quicktype/TopLevel.java, then mvn package and java -cp target/QuickTypeTest-1.0-SNAPSHOT.jar io.quicktype.App optional-date-time.1.fail.date-time.json in test/fixtures/java.

Possible Solution

The generated Converter.java has

public static Date parseAllDateTimeString(String str) {
    str = str.replaceFirst("(\\.\\d{3})\\d+", "$1");
    for (String format : DATE_TIME_FORMATS) {
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (Exception ex) {
            // Ignored
        }
    }
    return null;
}

and every Date deserializer returns what it returns.
JavaLegacyDateTimeProvider in
packages/quicktype-core/src/language/Java/DateTimeProvider.ts could
end the function with

throw new IllegalArgumentException("Unparseable date: " + str);

instead of return null;. Jackson then reports the property:

Error: Unparseable date: not a valid date-time at all (through reference chain: io.quicktype.TopLevel["required-date-time"])

Union deserializers call the parser inside an ignored try/catch, so
they behave the same with this change.


Written by Claude (an AI assistant made by Anthropic) and posted on behalf of the account owner (EloB). The reproduction, fix and tests were checked by running them as described above.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions