OpenAPITools / OpenAPITools/openapi-generator
[java-client][jersey2] ignore getter when serializing
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Given this spec:
openapi: 3.0.1
info:
title: ping test
version: '1.0'
servers:
- url: 'http://localhost:9999/'
paths:
/ping:
post:
operationId: postPing
requestBody:
content:
'application/json':
schema:
$ref: "#/components/schemas/SomeObj"
responses:
'201':
description: OK
content:
'application/json':
schema:
$ref: "#/components/schemas/SomeObj"
components:
schemas:
SomeObj:
type: object
properties:
$_type:
type: string
id:
type: integer
format: int64
name:
type: string
It generates:
public class SomeObj {
@JsonProperty("$_type")
private String $type;
//...
public SomeObj $type(String $type) {
this.$type = $type;
return this;
}
/**
* Get $type
* @return $type
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
public String get$Type() {
return $type;
}
public void set$Type(String $type) {
this.$type = $type;
}
//...
}
Because the getter get$Type() looks a little bit different than the $type field annotated with @JsonProperty("$_type"), jackson serializes the object like this:
{ "$_type": "someValue", "$Type": "someValue", "id": 42, "name": "test"}
Because all fields are generated with a proper @JsonProperty, discovery of the getters should be disabled when doing the serialization.
This can be configured in the generated JSON class:
https://github.com/OpenAPITools/openapi-generator/blob/411199bc9954c14779201e569d5f475ffb3e4be0/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/JSON.java#L16-L30
By adding a disable command for:
- MapperFeature.AUTO_DETECT_CREATORS
- MapperFeature.AUTO_DETECT_FIELDS
- MapperFeature.AUTO_DETECT_GETTERS
- MapperFeature.AUTO_DETECT_IS_GETTERS
More info: https://stackoverflow.com/questions/25893985/jackson-how-to-serialize-only-annotated-properties
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 generated samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/JSON.java file and trace how its Jackson mapper configuration is produced. Check the requested MapperFeature settings and verify that a generated object serializes only the annotated properties, without the duplicate getter-derived property.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100