Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize swagger codegen to generate models that ignore null values

I'm using swagger-codgen to generate java model classes for my spring boot application and serialize them as json. By default these models would all include optional properties with null values.

I would like to configure the swagger-codgen for spring to include this annotation on top of all classes: @JsonInclude(Include.NON_NULL) so that null valued properties are not included in the serialized json.

How can i achieve this? Is there a configuration option or do i have to extend the spring codegen manually?

like image 213
samy Avatar asked Oct 20 '25 01:10

samy


2 Answers

You can configure that in your application.yaml :

spring:
   jackson:
      default-property-inclusion: NON_NULL
like image 77
Lamine Mbn Avatar answered Oct 22 '25 05:10

Lamine Mbn


One way of achieving that would be modifying the pojo template for Java Spring by adding the annotation. This template is used to generate the models.

like image 37
moondaisy Avatar answered Oct 22 '25 04:10

moondaisy