OpenAPITools / OpenAPITools/openapi-generator
[BUG][CSHARP][PHP] Could not process model 'Dog.Type'
Nobody has claimed this yet.
- 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
- Add ".Type" or ".Any" to the types with
allOfdefinitions (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
- Specify the full component path in
InlineModelResolver.flattenComposedChildren.
Currently:
Schema schema = new Schema().$ref(innerModelName);
Proposed:
Schema schema = new Schema().$ref("#/components/schemas/" + innerModelName);
- Change
set$refinSchema.classto 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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