Ref in OpenAPI not working
See original GitHub issueDescribe the bug A clear and concise description of what the bug is.
To Reproduce
Example schema:
# openapi.yaml
components:
schemas:
Foo:
allOf:
- $ref: "test.yaml#/Test"
# test.yaml
TestNested:
type: object
properties:
test_string:
type: string
Test:
allOf:
- $ref: "#/TestNested"
Used commandline:
$ datamodel-codegen --input openapi.yaml --input-file-type openapi > ./models.py
Expected behavior
The $ref
in test.yaml/Test
resolves to test1.yaml#/TestNested
Version:
- OS: Ubuntu 20.04.1 LTS
- Python version: 3.8.10
- datamodel-code-generator version: [e.g. 22]
Additional context
The errors logs
Traceback (most recent call last):
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/__main__.py", line 431, in main
generate(
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/__init__.py", line 346, in generate
results = parser.parse()
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 427, in parse
_, sorted_data_models, require_update_action_models = sort_data_models(
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 115, in sort_data_models
return sort_data_models(
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 115, in sort_data_models
return sort_data_models(
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 115, in sort_data_models
return sort_data_models(
[Previous line repeated 97 more times]
File "/home/ubuntu/gordian/gordian-airline-api/lib/python3.8/site-packages/datamodel_code_generator/parser/base.py", line 174, in sort_data_models
raise Exception(f'A Parser can not resolve classes: {unresolved_classes}.')
Exception: A Parser can not resolve classes: [class: test.yaml#/Test references: frozenset({'test.yaml#/TestNested'})], [class: openapi.yaml#/components/schemas/Foo references: frozenset({'test.yaml#/Test'})].
The below change seems to remove the errors
- - $ref: "#/TestNested"
+ - $ref: "test.yaml#/TestNested"
Initially I thought this may be a problem with the ref path resolution, however from the error logs the ref path seems to be resolved correctly. And I’m not entirely sure if this problem arises from the ref being nested or something else.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Using $ref
A common misconception is that $ref is allowed anywhere in an OpenAPI specification file. Actually $ref is only allowed in places where the...
Read more >Examples referenced with $ref are not rendered correctly
I think $ref should work in that case too. Clearly, passing a 10-KB piece of JSON example as a string is very problematic....
Read more >Nested reference not working in Swagger UI / OpenAPI 3
The issue might be caused by YAML anchors. OpenAPI has a built-in way to reference definitions and do model composition - $ref and...
Read more >Using $ref to reuse code fragments in your OpenAPI ...
When IBM API Connect for IBM Cloud processes the source API definition file, the $ref field is replaced with the contents of the...
Read more >Swagger $ref won't work
Using swagger-editor and can use "$ref" in the swagger definition 2.0 and generate java code which is perfectly fine.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @koxudaxi, I know I’m very late but thanks a lot!
@shreyas44 I have released a new version
0.11.12
which is fixed the problem.