question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parser code uses old openapi2 "definitions" instead of "components/schemas"

See original GitHub issue

I am debugging this package, which explodes parsing a remote reference openapi.

Traceback (most recent call last):
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/__main__.py", line 574, in main
    generate(
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/__init__.py", line 371, in generate
    results = parser.parse()
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/base.py", line 460, in parse
    self.parse_raw()
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/openapi.py", line 397, in parse_raw
    self.parse_raw_obj(
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1180, in parse_raw_obj
    self.parse_obj(name, JsonSchemaObject.parse_obj(raw), path)
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1200, in parse_obj
    self.parse_ref(obj, path)
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1141, in parse_ref
    self.parse_ref(property_value, path)
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1120, in parse_ref
    self.resolve_ref(obj.ref)
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1109, in resolve_ref
    self._parse_file(
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1324, in _parse_file
    models = get_model_by_path(raw, object_paths)
  File "/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 54, in get_model_by_path
    return get_model_by_path(schema[keys[0]], keys[1:])
KeyError: ''

I am sure I am on the latest version:

/Users/tommy/.pyenv/versions/3.10.6/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/datamodel_code_generator/parser/jsonschema.py", line 54, in get_model_by_path

When I open this file, I see lots of mentions of #/definitions, but thats openapi2 lingo as noted at the bottom of the page here: https://swagger.io/docs/specification/components/

OpenAPI 2.0                    OpenAPI 3.0
'#/definitions/User'         → '#/components/schemas/User'

However, there are 0 usages of #/Components

Does this package support OpenAPI3, or am I invoking it wrong? I used:

datamodel-codegen --input-file-type openapi --input dataapi/openapi.yaml --output model.py

DEBUGGING:

The remote reference file we are trying to parse is actually public, here: http://merq-api-assets.s3-website-us-east-1.amazonaws.com/swagger_components.yaml

the paths is showing up as:

['', 'merq-api-assets.s3-website-us-east-1.amazonaws.com', 'swagger_components.yaml#', 'components', 'schemas', 'AirflowConfig']

However, 'swagger_components.yaml#', 'components', does not look right

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
koxudaxicommented, Nov 3, 2022

The code generator doesn’t support OpenAPI 2(a.k.a Swagger). we should update parser/openapi.py for OpenAPI. I will request to review it with you after I create the PR.

0reactions
koxudaxicommented, Nov 8, 2022

@tommyjcarpenter I have released the new version as 0.13.5 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

$refs in parameter is not processed · Issue #24 - GitHub
Yes, I'm working on it. I must resolve some problems. datamodel-code-generator don't parse /components/parameters (fastapi-code-generator use ...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
An OpenAPI definition can then be used by documentation generation tools to display the API, code generation tools to generate servers and clients...
Read more >
Step 5: The components object (OpenAPI tutorial)
The components object is unique from the other objects in the OpenAPI specification. In components, you store re-usable definitions that ...
Read more >
OpenAPI Specification v3.0.3 | Introduction, Definitions, & More
The OpenAPI Specification allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model ...
Read more >
An adventure in OpenAPI V3 code generation | Mux blog
We found using Examples in our OpenAPI definitions was also very helpful as ... Generator 4 was using an updated version of swagger-parser, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found