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:
|
public JSON() { |
|
mapper = new ObjectMapper(); |
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
|
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false); |
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
|
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); |
|
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); |
|
mapper.setDateFormat(new RFC3339DateFormat()); |
|
ThreeTenModule module = new ThreeTenModule(); |
|
module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT); |
|
module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME); |
|
module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME); |
|
mapper.registerModule(module); |
|
} |
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
Given this spec:
It generates:
Because the getter
get$Type()looks a little bit different than the$typefield 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:
openapi-generator/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/JSON.java
Lines 16 to 30 in 411199b
By adding a disable command for:
More info: https://stackoverflow.com/questions/25893985/jackson-how-to-serialize-only-annotated-properties