Feature request: $ref from local file
See original GitHub issueFirst of - very useful library/tool - thanks for making it!
I have a set of OpenAPI files (from these 3GPP specification) which I want to use.
In these files, many $ref
s point to components from local files.
For example:
'400':
$ref: 'TS29571_CommonData.yaml#/components/responses/400'
or
dnais:
type: array
items:
$ref: 'TS29571_CommonData.yaml#/components/schemas/Dnai'
minItems: 1
I see that the generator supports remote references but it does not support local files. It won’t be too hard to search-and-replace these references - assuming the files are hosted somewhere, or you want to make the effort to host them yourself, which is not always the case… so maybe it would be good to support such local references.
I’ve look into the code and it seems a few changes are required.
In this library, in get_ref_body
(fastapi_code_generator/parser.py, in line 40) - need to add another case to identify references to a file. I manually tested the following code:
RE_FILE_REF: Pattern[str] = re.compile(r"^.*\.ya?ml#") # at the top
elif RE_FILE_REF.match(ref):
# a new case - get ref body from local file
filename, path = ref.rsplit('#/', 1)
ref_body = openapi_model_parser._get_ref_body(filename)
return get_model_by_path(ref_body, path.split('/'))
I thought this might be enough, and it does work if your cwd
is where the files are located. Otherwise, _get_ref_body_from_remote
fails because of the way it is currently implemented.
I tried to understand what would be the right approach from here but I am not sure - I couldn’t find (yet) a reference to the original file available in parsing another - with the original file name, the $ref-ed file could either be in cwd
or relative to the original file.
I sort of got the project to run locally on my machine but I run into issues. I am not sure how quickly I can get everything to run (tests etc).
I’d be happy to help and I will try to setup my computer for development…
Thanks!
Tomer
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top GitHub Comments
@tomercagan Thank you for creating this issue and giving workarounds 😄
I explain why this project hasn’t supported $ref from the local file yet. I’m working on developing datamodel-code-generator aggressively. A lot of user uses datamodel-code-generator that to generate models from JSONSchema/OpenAPI, etc… They request resolving complex $ref and modular models. I clear this problem and request step by step. I will bring the feature into the fastapi-code-generator after I implement these features in the datamodel-code-generator.
For example, I want to support HTTP/HTTPS file directly. https://github.com/koxudaxi/datamodel-code-generator/issues/289#issuecomment-751488925
However, I always welcome any PR. If we can keep unittest then, I accept the PR. Because This project is used in production. We have to keep the same output from the same input file.
@koxudaxi - I have created a patch to add local ref support along with relevant tests.
When I run the unit tests (./scripts/test.sh) I am getting a failure in one of the existing tests:
Initially I thought I broke something but this happens (to me) on a clean clone without my changes…
I have have a commit ready to be PRd but not sure if I should proceed because of this (seemingly unrelated) failure