[JAVA] Bug generating model when discrimator is "_type" and a property called "type" also present in response schema
Created by: ahsanfz
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?
Description
So I am posting a snippet of a message from my schema. The problem with this schema is that the discriminator begins with an underscore followed by a name that is also a property in this particular message i.e. _type and type are present in this schema.
I can't change the discriminator to some other value as this will break existing client codes and we don't want to break existing client at the cost of being freshly able to generate open api clients.
So when I generate the code for my open-api specifications, I get an uncompilable java code.
openapi-generator version
openapi-generator-cli-5.1.1
OpenAPI declaration file content or url
Following is a cut down definition that demonstrates my problem:
openapi: 3.0.1
info:
title: Rest API
description: "This is the Restful API server."
version: 1.0.0
servers:
- url: http://localhost:8080/
description: Generated server url
paths:
/test:
get:
summary: Summary
description: Description
operationId: test
responses:
"200":
description: OK
content:
'*/*':
schema:
$ref: '#/components/schemas/Message'
components:
schemas:
Message:
required:
- _type
- text
type: object
properties:
type:
type: string
description: Type of this message.
enum:
- STATUS
- TEXT
- ERROR
- PROGRESS
subType:
type: string
description: Sub-type of this message.
enum:
- STATUS
- LOCK
- UNKNOWN
_type:
type: string
discriminator:
propertyName: _type
Command line used for generation
openapi-generator-cli generate
--input-spec "${DIR}"/api-docs.yaml
--generator-name java
--output "${JAVA_SRC_DIR}"/rest_client_with_resttemplate
--verbose
--config "${JAVA_CONFIG_DIR}"/resttemplate.json
--global-property debugModels=true
Contents of resttemplate.json:
{
"dateLibrary": "java8",
"library": "resttemplate",
"modelPackage": "model",
"apiPackage": "api",
"invokerPackage": "restapi.client"
}
Steps to reproduce
Generate the code given the command line and load the project in any editor. The Message.java file generated will show the problem how it is uncompilable,
Generated Code Description
The generated code is tripping on _type and type.
It generates wrongly as below:
@JsonPropertyOrder({
Message.JSON_PROPERTY_TYPE,
Message.JSON_PROPERTY_SUB_TYPE,
Message.JSON_PROPERTY_TYPE
})
public static final String JSON_PROPERTY_TYPE = "type";
private TypeEnum type;
public static final String JSON_PROPERTY_TYPE = "_type";
protected String type;
public String getType() {
return type;
}
public String getType() {
return type;
}
whereas it should be maybe something like:
@JsonPropertyOrder({
Message.JSON_PROPERTY_TYPE,
Message.JSON_PROPERTY_SUB_TYPE,
Message.JSON_PROPERTY__TYPE
})
public static final String JSON_PROPERTY_TYPE = "type";
private TypeEnum type;
public static final String JSON_PROPERTY__TYPE = "_type";
protected String _type;
public TypeEnum getType() {
return type;
}
public String get_Type() {
return _type;
}
Suggest a fix/enhancement
I looked at the moustache templates. No expert here, but maybe the "nameInSnakeCase" is the culprit in pojo.moustasche. I just searched the string "JSON_PROPERTY_" and the following lines poped up:
{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}}
Must be other places also as variable is wrong at a couple of places.
Let me know if something is missing in bug report.