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?

0190198
  • 1,667
  • 16
  • 23

3 Answers3

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.

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
  • 3
    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. – 0190198 Apr 02 '19 at 15:09
  • @SairamKukadala Great, I may have missed it. But I have now included some documentation which could help you. – Laksitha Ranasingha Apr 02 '19 at 15:37
  • 1
    This still does not fix the problem. Where is the documentation you said you added? – Lucas Coppio May 08 '20 at 12:35
  • @LucasScoppio I added the doc reference. Can you elaborate on what's not working gor you? – Laksitha Ranasingha May 08 '20 at 13:05
  • 1
    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.0 – Lucas Coppio May 08 '20 at 20:30
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;

}
  • 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](/help/how-to-answer). – Community May 11 '22 at 12:30
0

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

sendon1982
  • 9,982
  • 61
  • 44