Can we generate @JsonFormat on model variable using swagger-generater?
Asked Answered
F

3

14

I have a variable in yml file

startDate:
 type:string
 format:date

I'm using swagger-generater to generate java code from yml.

It's generating a startDate variable as below

@JsonProperty("startDate")
private LocalDate startDate = null; 

But I need as below

@JsonProperty("startDate")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate startDate = null; 

Can someone help me on this?

Foregut answered 29/3, 2019 at 21:24 Comment(0)
C
2

You can add x-field-extra-annotation to add anything annotation you need. Document is here: https://openapi-generator.tech/docs/generators/java

startDate:
 type:string
 format:date
 x-field-extra-annotation: '@com.fasterxml.jackson.annotation.JsonFormat(shape = com.fasterxml.jackson.annotation.JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")'

Then generated model class will have annotation JsonFormat

Canoodle answered 19/6, 2023 at 1:8 Comment(1)
Thank you very much! Maybe you know, is it possible to add "import com.fasterxml.jackson.annotation.JsonFormat;" in generated java code?Discophile
P
0

The problem here is you are trying to serialize a Java 8 LocalDate using @JsonFormat without using right jackson module/dependency. If you have a look the annotation doc, it says;

Common uses include choosing between alternate representations -- for example, whether Date is to be serialized as number (Java timestamp) or String (such as ISO-8601 compatible time value) -- as well as configuring exact details with pattern() property.

https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonFormat.html

There's no proper documentation in the swagger codegen project about how to specify language specific configuration options, I have only seen those config options in the following ticket;

https://github.com/swagger-api/swagger-codegen/issues/7795

As per the that issue, you could force Swagger codegen to use java8 dateLibrary.

Powdery answered 30/3, 2019 at 0:40 Comment(5)
Your solution definitely works if in case models are hand written but in my case model classes are generated by swagger-codegen. So I can't add @@JsonSerialize in model class. I tried to add jackson-datatype-jsr310 dependency but no luck. As stated in my question, I'm looking for a way to generate @JsonFormat tags by swagger-codegen.Foregut
@SairamKukadala Great, I may have missed it. But I have now included some documentation which could help you.Powdery
This still does not fix the problem. Where is the documentation you said you added?Steiner
@LucasScoppio I added the doc reference. Can you elaborate on what's not working gor you?Powdery
Sure, swagger codegen (with openapi3.0 file) does not understand fields marked as type: string format: date-time as rfc3339 or anything close to that, so the client if loaded with resttemplate will send an int timestamp instead in the field, if the date library is changed to localdate-time it will instead send an array with the date on each position. Also the rest template simply isnt working anymore with array query parameters for requests (it simply send the first item in the list instead of a full list). Had problems with multiple configs, in the end had to fall back to swagger 2.0Steiner
F
0

you can try desarialize with this way:

ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setDateFormat(df);
JSONObject jsonObject=new JSONObject(new Oferta().fechaInicio(new Date()));
String json="";
try{
    json=objectMapper.writeValueAsString(new Oferta().fechaInicio(new Date()).fechaAceptacion("15/554/20"));
    jsonObject=new JSONObject(json);
}catch (Exception e){

}



public class Oferta {
    @JsonProperty("fechaInicio")
    private Date fechaInicio = null;
    @JsonProperty("fechaAceptacion")
    private String fechaAceptacion = null;

}
Fraternal answered 11/5, 2022 at 3:48 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Koeninger

© 2022 - 2025 — McMap. All rights reserved.