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  
2
  • 1
    @Fazal before adding tags please review the tag wiki. api excerpt clearly says : DO NOT USE Commented Jan 19, 2023 at 4:20
  • thank you for clarifying my misunderstanding @Ruben Sir Commented Jan 19, 2023 at 7:16

2 Answers 2

7
+50

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"
Sign up to request clarification or add additional context in comments.

2 Comments

How to add property to the annotation? Like- @JsonInclude(Include.NON_NULL)
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?
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)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.