Invalid code generated for missing discriminator property, but validation passes

Open
#1,095 14 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java
Domain
api, tooling

Research direction

Reproduce the issue with test2.json and rpdmcpp.config by running the shown validate and generate commands, then inspect the generated Base.java. Trace the validation path for the discriminator property; done means validation reports the missing property instead of accepting the spec that produces non-compiling Java code.

Written by the indexing model from the issue text.

Description

Feature: Composition / Inheritance Issue: Bug
Description

A spec with missing discriminator property passes validation, but generates invalid (Java) code.

openapi-generator version

3.3.0-SNAPSHOT

OpenAPI declaration file content or url
{
    "openapi": "3.0.0",
    "info": {
        "description": "blah",
        "version": "1.0.0",
        "title": "blah"
    },
    "paths": {
        "/test": {
            "post": {
                "tags": [
                  "test"
                ],
                "operationId": "testOp",
                "responses": {
                    "405": {
                        "description": "Invalid input"
                    }
                },
                "requestBody": {
                    "$ref": "#/components/requestBodies/Test"
                }
            }
        }
    },
    "components": {
        "requestBodies": {
            "Test": {
                "content": {
                    "application/json": {
                        "schema": {
                            "$ref": "#/components/schemas/Test"
                        }
                    }
                }
            }
        },
        "schemas": {
            "Base": {
                "properties": {
                    "status": {
                        "type": "string"
                    }
                },
                "discriminator":{
                    "propertyName": "type"
                }
            },
            "Test": {
                "allOf":[
                    { "$ref": "#/components/schemas/Base" },
                    {
                        "type":"object",
                        "properties": {
                            "name": {
                                "type": "string"
                            }
                        }
                    }
                ]
            }
        }
    }
}

rpdmcpp.config is

{
	"declspec" : "DL_GLOBAL_CLASS",
	"appName" : "RPDM",
	"appDescription" : "RedPoint Data Management services"
}
Command line used for generation

Validate succeeds

java -cp openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator validate -i test2.json
Validating spec (test2.json)
No validation issues detected.

But java code generation:
java -cp openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator generate -v -o genjava -g java -i test2.json -c rpdmcpp.config
produces non-compiling code. The problem is that the type field is not defined. In Base.java:

  public Base() {
    this.type = this.getClass().getSimpleName();
  }
Suggest a fix/enhancement

Validation should flag the spec as being in error.

Dominant language
Java
Stars
26.8k
Forks
7.7k
Avg merge
1d 6h
Merged PRs (30d)
100

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.

More from OpenAPITools/openapi-generator

All issues in OpenAPITools/openapi-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.