swagger-api / swagger-api/swagger-codegen

[DART] Parsing null DateTime to JSON

Open
#9,260 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I'm not sure yet if this is a bug, or if I'm doing something wrong.

I've checked the dart/class.mustache file, and I think there lies the problem. When parsing a null DateTime property to json, it sets as an empty String. If I try to parse the generated json string, I get an exception when trying to DateTime.parse because the empty string is not an valid date.

Swagger-codegen version

2.4.2

Swagger declaration file content or url
{
    "type": "object",
    "properties": {
        "dateExample": {
            "type": "string",
            "format": "date-time",
            "description": "DateExample",
            "allowEmptyValue": false
        }
    },
    "title": "ExampleModel",
    "description": "Response model"
}
Command line used for generation

java -jar /swagger-codegen-cli.jar generate -i swagger.json -l dart -o sdk/dart -c deploy/swagger-generator.json -DbrowserClient=false

Steps to reproduce
  1. Generate a dart client having a model with a date property
  2. Parse the example model to json
  3. Parse the given json back to the model. The error should occur now, as mentioned before, from a "DateTime.parse"
Related issues/PRs
Suggest a fix/enhancement

Change this line https://github.com/swagger-api/swagger-codegen/blob/f147928d82db0352761f93a7d947924a77160496/modules/swagger-codegen/src/main/resources/dart/class.mustache#L41

to:
{{#isDateTime}}'{{name}}': {{name}} == null ? null : {{name}}.toUtc().toIso8601String(){{^-last}},{{/-last}}{{/isDateTime}}{{^isDateTime}}'{{name}}': {{name}}{{^-last}},{{/-last}}{{/isDateTime}}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/dart/class.mustache at the referenced line and generate a Dart client using the provided command and schema. Reproduce the null DateTime serialization and round-trip described in the issue; done means the generated JSON preserves a null date-time value and can be parsed back without a DateTime.parse exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.