JsonIgnore using Open API spec
Asked Answered
K

2

10

I use OpenAPI spec to generate Java POJOs. What do I need to specify in Open API yaml to generate the equivalent of below POJO ?

...
@JsonIgnore
public String ignoredProperty;
...

I have the yaml spec as below

openapi: 3.0.0
info:
  title: Cool API
  description: A Cool API spec
  version: 0.0.1
servers:
  - url: http://api.cool.com/v1
    description: Cool server for testing
paths:
  /
  ...
components:
  schemas:
    MyPojo:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        # I want the below attribute to be ignored as a part of JSON 
        ignoreProperty:  
          type: string  
Kelsy answered 18/11, 2020 at 17:44 Comment(2)
@Fazal before adding tags please review the tag wiki. api excerpt clearly says : DO NOT USEVariola
thank you for clarifying my misunderstanding @Ruben SirForgotten
D
7

the openapi generator supports vendor extensions. Specifically, for the Java generator, it supports the following extensions as of the time of writing. However, an up-to-date list can be found here.

Extension name Description Applicable for Default value
x-discriminator-value Used with model inheritance to specify value for discriminator that identifies current model MODEL
x-implements Ability to specify interfaces that model must implements MODEL empty array
x-setter-extra-annotation Custom annotation that can be specified over java setter for specific field FIELD When field is array & uniqueItems, then this extension is used to add @JsonDeserialize(as = LinkedHashSet.class) over setter, otherwise no value
x-tags Specify multiple swagger tags for operation OPERATION null
x-accepts Specify custom value for 'Accept' header for operation OPERATION null
x-content-type Specify custom value for 'Content-Type' header for operation OPERATION null
x-class-extra-annotation List of custom annotations to be added to model MODEL null
x-field-extra-annotation List of custom annotations to be added to property FIELD null
x-webclient-blocking Specifies if method for specific operation should be blocking or non-blocking(ex: return Mono<T>/Flux<T> or return T/List<T>/Set<T> & execute .block() inside generated method) OPERATION false

You can use the x-field-extra-annotation vendor extension listed above to add annotations to any field. So, for your example, you can add the following:

openapi: 3.0.0
info:
  title: Cool API
  description: A Cool API spec
  version: 0.0.1
servers:
  - url: http://api.cool.com/v1
    description: Cool server for testing
paths:
  /
  ...
components:
  schemas:
    MyPojo:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        # I want the below attribute to be ignored as a part of JSON 
        ignoreProperty:  
          type: string  
          x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonIgnore"
Disjointed answered 16/1, 2023 at 22:42 Comment(2)
How to add property to the annotation? Like- @JsonInclude(Include.NON_NULL)Indihar
This doesn't work. I still see the field in response of API. Apart from just adding x-field-extra-annotation do we need to do any additional handling in JSON serializer?Covey
A
1

I had the same problem today, current answer as of today doesn't work anymore for me with spring (has same vendor extension) as it looks like meanwhile jackson changed the behaviour that method accessor JSONProperty (which is at getter level generated) overrides JsonIgnore from field level.

However with the following annotation on field level i could make it work:

x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonProperty(access = JsonProperty.Access.WRITE_ONLY)"
Academia answered 16/5 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.