Datamodel-code-generator: Code generation fails with SchemaGenerationError: Could not find matching schema type for object: datetime.datetime

Created on 25 Dec 2020  路  4Comments  路  Source: koxudaxi/datamodel-code-generator

Describe the bug
I am trying to generate pydantic models for Hypothes.is' OpenAPI specification in YAML format. However, this fails with error genson.schema.node.SchemaGenerationError: Could not find matching schema type for object: datetime.datetime(2019, 1, 3, 19, 46, 9, 334539, tzinfo=datetime.timezone.utc).

To Reproduce

The YAML schema is available at https://h.readthedocs.io/en/latest/api-reference/hypothesis-v1.yaml.

Used commandline:

$  curl -s https://h.readthedocs.io/en/latest/api-reference/hypothesis-v1.yaml | datamodel-codegen  --input-file-type=yaml --debug

Expected behavior
I hoped to generate some pydantic models 馃檭

Version:

  • OS: WSL 2 running Ubuntu 20.04.1 LTS (Focal Fossa)
  • Python version: 3.8.5
  • datamodel-code-generator version: 0.6.9

Additional context
Also tried in Windows 10 with Python 3.9.1, with identical results.
On both cases, datamodel-code-generator was installed using pipx and with the datamodel-code-generator[http] dependencies.

released

Most helpful comment

I just confirmed that convertion works as expected for the mentioned OpenAPI, with the optional dependencies datamodel-code-generator[http] installed, --input-file-type=openapi as the input file type and the brand new --url parameter:

$ datamodel-codegen --url "https://h.readthedocs.io/en/latest/api-reference/hypothesis-v1.yaml" --input-file-type openapi

Closing the issue. Thank you very much for the support!

All 4 comments

@bcbernardo
Thank you for creating this issue.
OK, I will fix it.

@bcbernardo
I have checked this YAML.
I understand why you got the error.
--input-file-type=yaml is wrong.
This file is an OpenAPI file. you have to type openapi. --input-file-type=openapi

Also, I got another error that it can't resolve reference files.
The OpenAPI $ref is a relative path.
For example, $ref: './schemas/errors.yaml#/Error'
The code-generator tries to find the error.yaml in./shemas.
However, The schema doesn't exist locally, and it is in https://~.

There is a workaround.
You should download all $ref files and move to the ./schemas directory.
You may need to write a small shell for getting all schema files. :(

I think the best way that the code-generator supports the https://~ protocol directly as an input.
The feature will resolve the relative file path on the HTTPS server.

Hi @bcbernardo
I add the option --url on version 0.9.1 which we can give URL as an input file.

$ pip install -U  "datamodel-code-generator[http]"
$ datamodel-codegen --url https://h.readthedocs.io/en/latest/api-reference/hypothesis-v1.yaml --input-file-type openapi

I just confirmed that convertion works as expected for the mentioned OpenAPI, with the optional dependencies datamodel-code-generator[http] installed, --input-file-type=openapi as the input file type and the brand new --url parameter:

$ datamodel-codegen --url "https://h.readthedocs.io/en/latest/api-reference/hypothesis-v1.yaml" --input-file-type openapi

Closing the issue. Thank you very much for the support!

Was this page helpful?
0 / 5 - 0 ratings