OpenAPITools / OpenAPITools/openapi-generator

[BUG] Using external references in OpenAPI spec

Open
#19,323 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist

If external file references are used in an OpenAPI spec file, an error is thrown by a utility function. Failed to get the schema name: ../../shared/requests/submit.yaml#/components/schemas/SubmitSuccessResponseV1
(This value is particular to my input. In general, it prints the name of the external reference file#/component-name)

It originates here: https://github.com/OpenAPITools/openapi-generator/blob/4b493358a882171781e239af2582679ec39aa552/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java#L393

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version

I'm building the tip of master brach at 4b49335

OpenAPI declaration file content or url

I can reproduce the issue with this file: https://github.com/ripple/rippled-api-spec/blob/main/open_api/json_api.yaml

I suspect that any OpenAPI input file that uses $ref in an external file, will see this problem. I'm using OpenAPI v3.1.0

Generation Details
  1. mvn clean package -DSkipTests
  2. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g python -i ../rippled-api-spec/open_api/json_api.yaml -o sample-python-out-jul25-fresh --skip-validate-spec

Here is the observed log from the code-generator:
external-ref.log

Steps to reproduce
Related issues/PRs

couldn't find related issues

Suggest a fix
public static String getSimpleRef(String ref) {
        // a reference to an external file
        if (ref != NULL && ref.contains(".yaml#/")) {
            // strip the filePath portion of the reference, only retain the #/component or #/definition portion
            ref = ref.subString(ref.lastIndexOf(".yaml#/") + 5)
        }
        
        // ... continue with the rest of the function as-is

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java around line 393 and reproduce with ripple/rippled-api-spec's open_api/json_api.yaml. Run the Maven build and Python generation command from the issue, then inspect how the external $ref is parsed. Done means generation handles the external reference without the reported schema-name error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.