swagger-api / swagger-api/swagger-codegen
[Question -JAVA] Not able to generate proper getter and setter method names, when the field name starts with "_".
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
I am not able to generate proper getter setter in java POJO class for fields that name starts with @ (like @Typetype, )in my open API 3.0 schema definition.
I am using swagger code-gen version: 2.4.4, java 11, spring boot 2.2.6
Please find the open API sample (just the component/schema part ) as follows:-
"components": {
"schemas": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
},
"@testType"{
"type": "string"
}
}
generated field "@testType" in Order class
@JsonProperty("@testType")
private String _atTestType = null;
generate getter setter for the field "@testType" in Order class
@ApiModelProperty(value="......")
@JsonProperty("@testType")
public String getAtTestType(){
return _atTestType ;
}
@JsonProperty("@testType")
public String setAtTestType(String _atTestType){
return this. _atTestType = _atTestType ;
}
But as per Java naming convention, getter and setter name should be as follows:
@ApiModelProperty(value="......")
@JsonProperty("@testType")
public String get_atTestType(){
return _atTestType ;
}
@JsonProperty("@testType")
public String set_atTestType(String _atTestType){
return this. _atTestType = _atTestType ;
}
As a result, when we pass the value for @testType in request it is not getting bound the filed in Order POJO. We are using Jackon for serialization, deserialization, and binding.
I have tried updating pojo.mustache for not able to achieve a solution.
Please help us with a resolution.
In Java, it is allowed to have a variable starting with "_".
-->Related issues/PRs
I could not find any.
--> Suggest a fix/enhancement
We can try to accommodate logic for fields that starts with "_" for generating a proper java understood getter and setter method name for them.
Or is there a way to tweak the pojo.mustache file for generating that
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 the OpenAPI schema for Order and the generated Order POJO shown in the issue, then inspect pojo.mustache to trace how fields beginning with @ become Java fields and accessor names. Compare the generated accessors with Jackson binding behavior and verify that the resulting getter and setter names preserve the intended property mapping.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100