openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

fixes Object of type date is not JSON serializable error

Open bgunebakan opened this issue 10 months ago • 3 comments

The to_json function in the Python generator currently throws a TypeError when attempting to serialize objects that contain date types such as datetime.date. This issue affects all Python clients generated by the OpenAPI generator where the schema contains date types. The error encountered is: "Object of type date is not JSON serializable."

Related Issue: #18397

Proposed Changes: This update modifies the to_json method in the model_generic.mustache template to incorporate a more dynamic approach to excluding fields during serialization. This approach ensures that fields that could potentially cause serialization errors, such as read-only fields or additional properties, are automatically excluded based on the model configuration.

PR checklist

  • [X] Read the contribution guidelines.
  • [X] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • [X] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [X] File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

bgunebakan avatar Apr 24 '24 12:04 bgunebakan

thanks for the PR. please follow step 3 to update the samples so that the CI can verify the change.

wing328 avatar Apr 25 '24 00:04 wing328

cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)

wing328 avatar Apr 30 '24 06:04 wing328

the python tests failed.

can you please take a look when you've time?

wing328 avatar Apr 30 '24 06:04 wing328

Hi. Thank you for this PR @bgunebakan and @wing328

I have looked into the failed tests.

  1. It seems like the majority (but not all) of the tests from the Petstore suite are failing due to the response format being different from the expected one, mainly the new response of the stringified json objects not having a whitespace before the value in a key value pair:
  • Expected by the test: "key": "value"
  • Provided by the code: "key":"value"
  1. Others are missing some additional "key":"value" pairs. There I am not sure what the reason is. Could it be due to the additional logic of the excluded_fields ?

In case of 1), are these whitespaces between the semicolons and values necessary? Should the tests be adjusted, or the formatting of the returning object?

I am interested in fixing the issue of date-time serialisation. How could I best contribute in solving it in this case?

mythody avatar May 30 '24 14:05 mythody

In case of 1), are these whitespaces between the semicolons and values necessary? Should the tests be adjusted, or the formatting of the returning object?

It is not specifically defined, so the tests should be adjusted.

fa0311 avatar May 31 '24 05:05 fa0311