When building APIs with OpenAPI, managing date and time formats efficiently is crucial. If you need to generate Java models with LocalTime fields instead of the default OffsetDateTime
for date-time values, this guide is for you. In this post, you’ll learn how to customize your OpenAPI generation process with openapi-generator to create models that use Java’s LocalTime
from the java.time
package.
Why Use LocalTime?
LocalTime is ideal for representing a time-of-day without any time-zone information. It’s particularly useful for applications that require scheduling or logging times that are independent of geographic location. By using openapi-generator with the right configurations, you can ensure that your generated Java classes are clean, type-safe, and align with your project’s requirements.
OpenAPI Specification & Date Formats
OpenAPI supports several built-in formats for date and date-time strings. However, it does not natively support a pure time format (e.g., “HH:mm:ss”). Typically, using the date-time
format produces a Java type such as OffsetDateTime
when using the default configurations. To generate a LocalTime
field, you need to leverage the generator’s customization options.
Customizing openapi-generator for LocalTime
The solution is simple. You can instruct openapi-generator to map a custom format (for example, time
) to LocalTime
by using type mappings and import mappings. Here’s how you can do it:
Command Line Example
Run the openapi-generator-cli with the following parameters:
openapi-generator-cli generate -g java \
--type-mappings time=LocalTime \
--import-mappings LocalTime=java.time.LocalTime
This command tells the generator that any schema field defined with type: string
and format: time
should be generated as a LocalTime
field in Java. The --import-mappings
option ensures that the necessary import (java.time.LocalTime
) is added to the generated class.
Example OpenAPI Schema
Consider the following OpenAPI schema snippet for a Visit
object:
Visit:
type: object
properties:
visitor:
type: string
timeOfVisit:
type: string
format: time
With the type mappings in place, the generated Java class will look similar to this:
public class Visit {
private String visitor;
private LocalTime timeOfVisit;
// Getters and setters
}
Using Maven Plugin Configuration
If you’re using Maven, you can configure the plugin directly in your pom.xml
file to apply these settings automatically:
<configuration>
<typeMappings>
<typeMapping>time=LocalTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>LocalTime=java.time.LocalTime</importMapping>
</importMappings>
</configuration>
This configuration integrates seamlessly into your build process, ensuring that every generation of your Java models adheres to your custom date and time type requirements.
Keywords and Best Practices
- OpenAPI LocalTime generation
- openapi-generator LocalTime mapping
- Java LocalTime OpenAPI customization
- OpenAPI specification time format
- OpenAPI generator type mappings
These keywords can help you quickly find relevant discussions and documentation on implementing custom date and time formats with openapi-generator.
Conclusion
Generating LocalTime
fields from an OpenAPI specification is straightforward with the right configuration. By leveraging type mappings and import mappings in openapi-generator, you can customize your Java model classes to meet your application’s specific needs. This approach not only improves type safety but also aligns your generated code with modern Java best practices.
Feel free to share your experiences or ask questions in the comments below if you have any issues implementing this solution.
Meta Description: Learn how to generate LocalTime fields from your OpenAPI specification using openapi-generator. This guide covers command line and Maven configurations for Java projects.