Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate `LocalTime` from OpenAPI specification

I see that there is a date format for strings in OpenAPI, and that by using dateLibrary=java8 we can generate LocalDate fields by using openapi-generator.

But is there any way of producing LocalTime fields? There is no time format in OpenAPI and the date-time one produces OffsetDateTime.

EDIT: It's hard offering a reproducible example since the question is about something I can't do, but some illustrative example would be that I want something along the lines of:

A schema specification:

Visit:
  type: object
  parameters:
    visitor:
      type: string
    timeOfVisit:
      type: string
      format: time

But obviously the time format is not present in the OpenAPI specification. The generated code should be something like

public class Visit {
  private String visitor;
  private LocalTime timeOfVisit;

  // Getters, setters, etc
}

There must surely be some way for openapi-generator to produce this output, isn't it? I've found that there are some import-mappings that map LocalTime to org.joda.time.* so there seems to be a way of having it produce LocalTime types, but I haven't found it

like image 384
Haf Avatar asked Aug 25 '26 21:08

Haf


1 Answers

Solved it! Actually it was really easy, but being a beginner in OpenAPI it was hard finding a solution. Given the example in my question, I'd just have to run openapi-generator-cli as

openapi-generator-cli generate -g java --type-mappings time=LocalTime

And voilà, it's done!

EDIT: This would make the field type become LocalTime, but the compiler will complain that the type is not imported. The import can be added using the previously (in the question) mentioned import-mappings, so the definitive command would be:

openapi-generator-cli generate -g java --type-mappings time=LocalTime --import-mappings LocalTime=java.time.LocalTime

EDIT:

An alternative is to configure the plugin inside your pom.xml:

<configuration>
    <typeMappings>
        <typeMapping>time=LocalTime</typeMapping>
    </typeMappings>
    <importMappings>
        <importMapping>LocalTime=java.time.LocalTime</importMapping>
    </importMappings>
</configuration>
like image 124
Haf Avatar answered Aug 28 '26 11:08

Haf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!