OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP][PHP] Could not process model 'Dog.Type'

Open
#3,124 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When types with an allOf definition have names with ".Type" or ".Any" suffix, there is an error at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:470)

Actual Output

[main] ERROR o.o.codegen.utils.ModelUtils - Failed to obtain schema from null
[main] ERROR o.o.codegen.utils.ModelUtils - Failed to obtain schema from null
Exception in thread "main" java.lang.RuntimeException: Could not process model 'Dog.Any'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:470)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:919)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:396)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Caused by: java.lang.NullPointerException
        at org.openapitools.codegen.languages.AbstractPhpCodegen.toModelName(AbstractPhpCodegen.java:402)
        at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1755)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1196)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:465)
        ... 3 more

Expected Output

  • No errors
openapi-generator version

4.0.2-SNAPSHOT

OpenAPI declaration file content or url

https://gist.github.com/amiller-ims/21f6a4ec6acbb408774f629cdaf8b42c

Command line used for generation

Both of these command lines cause similar errors:

  • java -jar openapi-generator-cli.jar generate -g php -i petstore.yaml
  • java -jar openapi-generator-cli.jar generate -g csharp -i petstore.yaml
Steps to reproduce
  1. Add ".Type" or ".Any" to the types with allOf definitions (maybe any suffix with a period "."). For example, rename "Pet" to "Pet.Type":
definitions:
  Dog.Type:
    allOf:
      - $ref: '#/definitions/Pet.Type'
      - type: object
        properties:
          breed:
            type: string
Related issues/PRs

I searched both Issues and PRs but could not find this problem.

Suggest a fix
  1. Specify the full component path in InlineModelResolver.flattenComposedChildren.

Currently:

Schema schema = new Schema().$ref(innerModelName);

Proposed:

Schema schema = new Schema().$ref("#/components/schemas/" + innerModelName);
  1. Change set$ref in Schema.class to check for hash ("#") instead of period ("."):

Currently:

    public void set$ref(String $ref) {
        if ($ref != null && ($ref.indexOf(".") == -1 && $ref.indexOf("/") == -1)) {
            $ref = "#/components/schemas/" + $ref;
        }
        this.$ref = $ref;
    }

Proposed:

    public void set$ref(String $ref) {
        if ($ref != null && ($ref.indexOf("#") == -1 && $ref.indexOf("/") == -1)) {
            $ref = "#/components/schemas/" + $ref;
        }
        this.$ref = $ref;
    }

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 with InlineModelResolver.flattenComposedChildren and set$ref in Schema.class, then reproduce the failure using the linked petstore.yaml and the PHP or C# generation commands. Trace how allOf schemas named with ".Type" or ".Any" are resolved and verify that both generators complete without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java, openapi, php
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.