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.

Feature request: $ref from local file

See original GitHub issue

First 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 $refs 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
koxudaxicommented, Jan 30, 2021

@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.

2reactions
tomercagancommented, Feb 5, 2021

@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:

FAILED tests/test_generate.py::test_generate_custom_security_template[oas_file0] - 
AssertionError: assert '# generated ...:\n    pas...

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using $ref - Swagger
Local Reference – $ref: '#/definitions/myElement' # means go to the root of the current document and then find elements definitions and myElement one...
Read more >
GitLab CI/CD include examples
Use rules:if to conditionally include other configuration files based on the status of CI/CD variables. For example: include: - local: builds.yml rules: ...
Read more >
Git References - Git SCM
In Git, these simple names are called “references” or “refs”; you can find the files that contain those SHA-1 values in the .git/refs...
Read more >
Add ability to select existing Salesforce Files for images in ...
Feature Request : Add ability to select existing Salesforce Files for images in Lightning Knowledge articles. Content and Files.
Read more >
Work with Azure Functions Core Tools | Microsoft Learn
Your local functions can connect to live Azure services, and you can debug your ... To file a bug or feature request, open...
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