Generate `LocalTime` from OpenAPI specification
Asked Answered
H

3

5

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

Hungry answered 6/10, 2021 at 8:46 Comment(4)
You can use the OffsetDateTime and call myOffsetDateTime.toLocalTime().Mammalogy
It could work but it's not too effective, firstly, I'd have to do that in a lot of different places, and also, setting the parameter as date-time in the OpenAPI specification would communicate that it allows any combination of date and time, while it actually only works with time. I guess my wording wasn't very good, because what I'd like is to have a time parameter in my API and also in the generated code. (Maybe using a regex pattern for the string is my only choice?)Hungry
Sorry, I think it was not the wording but the lack of example values and code. Could you provide some minimal and reproducible example of the situation?Mammalogy
I've added a small example of what I needHungry
H
8

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>
Hungry answered 6/10, 2021 at 10:40 Comment(4)
OK, good... Because the specifiation includes only the two types you already mentioned: date-time and dateMammalogy
Is there a way to use the same thing using the openapi-generator-maven-pluginGondar
Yeah, use the following snippet inside your <configuration>...</configuration> block in your pom.xml: java <typeMappings> <typeMapping>OffsetDateTime=java.time.LocalDateTime</typeMapping> </typeMappings> <importMappings> <importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping> </importMappings> Sorry for the poor formatting. You might have to fully qualify OffsetDateTime in typeMapping too, but this worked for meHungry
@Haf: Can you please remove the snippet from your comment, it is not equivalent to the solution you provide in your answer ? I sent you an edit to your answer, to fix it.Cambium
A
3

If you are using spring with gradle you need to add this two properties to build.gradle

openApiGenerate() {
    generatorName = "spring"
    ...
    typeMappings = ["time": "LocalTime"]
    importMappings = ["LocalTime": "java.time.LocalTime"]
}

So this configuration will say that format: time will generate LocalTime type and java.time.LocalTime here is the source of this type.

Afrikah answered 30/5, 2023 at 14:44 Comment(0)
B
2

I have found that there is a dateLibrary option in the configOptions tag in the pom.xml. This way I didn't have to map manually between OffsetDateTime (which was being generated at first) and LocalDateTime.

If you add true to the configuration tag it will print out all the options.

dateLibrary
        Option. Date library to use (Default: threetenbp)
            joda - Joda (for legacy app only)
            legacy - Legacy java.util.Date (if you really have a good reason not to use threetenbp
            java8-localdatetime - Java 8 using LocalDateTime (for legacy app only)
            java8 - Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets "java8" to true
            threetenbp - Backport of JSR310 (preferred for jdk < 1.8)
Bakken answered 31/3, 2023 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.