OpenAPITools / OpenAPITools/openapi-generator

[BUG][ALL] Error obtaining the datatype from ref:#/paths/.... Default to 'object'

Open
#7,400 3 comments 2 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
  • 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

I was having problems using a bundled openapi.yaml or openapi.json file with this code generator. I tried a few different bundlers and found the same issue. The problem was uncovered when bundler resolves refs for schemas by placing the ref in the first path it finds directly in the path object. Subsequent refs to the same about are then rewritten to be local refs that point back to that first one found in the path object again.

When openapi generator reads this bundled openapi.yaml file, it can't follow the the local refs that contain '#paths' as shown in this warning:

[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#/paths/~1ui~1device~1delete/post/requestBody/content/application~1json/schema. Default to 'object'

This results in the generator putting "Object" in place of the correct Model just about everywhere in Java generation. I found this when using the Spring generators, but this is a larger issue. The only way I found to work around this problem externally was to have the bundler repeat all the refs (-r in swagger-cli). However, this resulted in the openapi generator repeating the Models over and over (by adding numbers after the name), which was also not ideal as we now have several models that are identical.

openapi-generator version

I have confirmed this issue in both v4.3.1 and the latest 5.0.0

OpenAPI declaration file content or url

Any bundled code that has multiple $refs to the same object will exhibit this issue. I have debugged to the very line of code and tested the fix (see below)

Steps to reproduce
  1. Created schema with multiple $refs to the same schema....body or response
  2. Run schema through swagger-cli bundle with -r flag
  3. Run through openapi-generator to see issue similar to this:

[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #/paths/~1ui~1user~1add/post/requestBody/content/application~1json/schema
[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#/paths/~1ui~1user~1add/post/requestBody/content/application~1json/schema. Default to 'object'

Which causes generated code to not put proper object types into code downstream.

Related issues/PRs

I haven't found any similar PRs, but your system is hard to search through.

Suggest a fix

I debugged the issue to the following file:

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Here is the offending function:


public static String getSimpleRef(String ref) {
        if (ref.startsWith("#/components/")) {
            ref = ref.substring(ref.lastIndexOf("/") + 1);
        } else if (ref.startsWith("#/definitions/")) {
            ref = ref.substring(ref.lastIndexOf("/") + 1);
        } else {
            once(LOGGER).warn("Failed to get the schema name: {}", ref);
            //throw new RuntimeException("Failed to get the schema: " + ref);
            return null;

        }

        try {
            ref = URLDecoder.decode(ref, "UTF-8");
        } catch (UnsupportedEncodingException ignored) {
        }

        // see https://tools.ietf.org/html/rfc6901#section-3
        // Because the characters '~' (%x7E) and '/' (%x2F) have special meanings in
        // JSON Pointer, '~' needs to be encoded as '~0' and '/' needs to be encoded 
        // as '~1' when these characters appear in a reference token.
        // This reverses that encoding.
        ref = ref.replace("~1", "/").replace("~0", "~");

        return ref;

 } 

The problem is that all of these bundled refs start with "#/paths/" which is not listed as one of the possibilities in this function. I have a local build where I added:


        if (ref.startsWith("#/components/")) {
            ref = ref.substring(ref.lastIndexOf("/") + 1);
        } else if (ref.startsWith("#/definitions/")) {
            ref = ref.substring(ref.lastIndexOf("/") + 1);
        } else if (ref.startsWith("#/paths/")) {
            ref = ref.substring(ref.lastIndexOf("/") + 1);
        } else {
            once(LOGGER).warn("Failed to get the schema name: {}", ref);
            //throw new RuntimeException("Failed to get the schema: " + ref);
            return null;
        }

and now the code works as expected. I can put this into a pull request if you like. I feel that this is not an uncommon use case and it's a fairly simple fix. I suggest this should be a high priority.

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, focusing on getSimpleRef. Reproduce the warning with a bundled OpenAPI document containing repeated refs under #/paths/, then run OpenAPI Generator. Done means the refs resolve without the datatype warning and generated Java code uses the correct model types instead of Object.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.