Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[java-client][jersey2] ignore getter when serializing #3573

Open
jmini opened this issue Aug 7, 2019 · 2 comments
Open

[java-client][jersey2] ignore getter when serializing #3573

jmini opened this issue Aug 7, 2019 · 2 comments

Comments

@jmini
Copy link
Member

jmini commented Aug 7, 2019

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

@auto-labeler
Copy link

auto-labeler bot commented Aug 7, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jmini
Copy link
Member Author

jmini commented Aug 13, 2019

Instead of configuring the mapper, an other approach is to use the@JsonAutoDetect annotation with visibility NONE.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant