Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #8804
Closed
Open
Issue created Feb 23, 2021 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][ELIXIR] Date format not generating correct code, and date-time format not generating code

Created by: tomjoro

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

This is actually 2 issues, but both would need to be considered at same time in order to fix.

For dates, and datetimes there are two date/datetime related formats in openapi:

  • date – full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21
  • date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z

An example of date format in json:

"closedDate":{"title":"closed date","type":"string","format":"date","example":"2020-04-20"},

The generated deserializer.ex cannot parse "2020-04-20", because DateTime.from_iso8601 doesn't work with a date only. You must use Date.from_iso8601

iex(1)> DateTime.from_iso8601("2020-04-20")
{:error, :invalid_format}

The generated deserializer.ex

def deserialize(model, field, :date, _, _options) do
    value = Map.get(model, field)
    case is_binary(value) do
      true -> case DateTime.from_iso8601(value) do
                {:ok, datetime, _offset} ->
                  Map.put(model, field, datetime)
                _ ->
                  model
              end
      false -> model
    end
  end

For date-time example from swagger:

"closedDate":{"title":"closed date","type":"string","format":"date","example":"2020-04-20"}, `
"createdDTime":{"title":"Created date time","type":"string","format":"date-time"}

The @type is generated correctly like this:

    :closedDate => Date.t | nil,
    :createdDTime => DateTime.t | nil,

But there is no deserializer generated for the date-time format in the decode part of the json.ex file.

defimpl Poison.Decoder, for: InvoiceLineJson do
  import Project.Deserializer
  def decode(value, options) do
    value
    |> deserialize(:closedDate, :date, nil, options)
  end
end
openapi-generator version

5.0.1

OpenAPI declaration file content or url
Generation Details

Run openapi-generator on a swagger that includes "date" fields and "date-time" fields.

Steps to reproduce

Run openapi-generator on a swagger that includes "date" fields and "date-time" fields.

Related issues/PRs

I don't see any other Elixir related PRs on this?

Suggest a fix

I'm happy to help fix this, but just thought I'd document this if anyone else is running into this problem? It seems quite fundamental to most openapi generation.

Fix proposal:

  • date format
    • Change deserializer.ex to use Date.from_iso8601
  • date-time format
    • add "date-time" struct to the deserializer to use DateTime.from_iso8601
    • generate a line in each json schema file to call the deserializer (same as date works already)

Problem with this fix:

This fix could be problematic for anyone who is using a swagger file that has specified date format and then actually sending a date-time, i.e. 2021-02-22T10:22:19Z, because Date.from_iso8601 will not parse a date-time in Elixir.

I guess we could inspect the string that is provided and make a determination if it is Date or DateTime by searching for a "T" in the string and call either Date.from_iso8601 or DateTime.from_iso8601. This single change to deserializer.ex would make the date format work, but the generator still needs to be updated for date-time.

Comments? Ideas?

Assignee
Assign to
Time tracking