You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Harold L. Brown, tkalmar, hgjd, David Scheers, Marinus Swanepoel, Marcel Overdijk, Tobias Trumm, mvpestretsov, Liv Fischer, AiryLenglet and 131 more
Reacted by Juan Antonio Breña Moral, Adrian, Thalys de Aguiar Gomes, Bertoldo Fonseca, Ivoš Apolenář, JY Cr, Alexandre Xavier, VladislavBakshanskij and Adam Saturna
Reacted by Bertoldo Fonseca, Alexandre Xavier and VladislavBakshanskij
It could even be a sensible default when using the Spring generator with useSpringBoot3, as that requires Java 17.
The model cannot be an enum or extend another class. Am I missing any other limitations?
Reacted by DEMARE Julien, Omar Aloraini, Lasth, Thalys de Aguiar Gomes, Anthony Defraine, Aryesia, j-stary, pharacyde, Gleb Ivashyn and Adam Saturna
It would be amazing if we got this improvement - specially when we set useSpringBoot3
One question though is: will the records DTOs cope well with swagger annotations?
Please don't write "plus one", it clutters the issue and generates notifications for subscribers. You can "thumbs up" the initial description.
I would really like to help implementing this feature, but I am overwhelmed after looking into the pojo mustache template. There seem to be so many special cases to consider (Jackson nullables for example).
So, this issue caught my attention a few months ago. And recently I started working on setting up a new project with OpenAPI generator and decided to give generating records as models a try.
I'm excited to share that this endeavor has been largely successful, and our project now leverages the immutable nature of records for its DTOs.
I'm here to share my findings, solutions, and templates with you. It's important to note, however, that while this approach has worked well for us, it might not be universally suitable, particularly due to some inherent limitations of records. In my experience, this strategy shows the most promise for simpler APIs, where the benefits of records truly shine.
Before diving into the solutions and templates, let's first unpack the limitations of records. Understanding these constraints is crucial for evaluating whether records are the right fit for your API development needs
Limitations:
1. Records do not allow inheritance:
OpenAPI spec allows inheritance in the API and so does the default template used by openapi generator for spring for example. Since records are technically a very special type of class that is final and immutable, it also extends the java. lang. Record class. Due to Java's nature of "single inheritance" (it can extend only one class), creating inheritance models with records can not be achieved.
I believe there is no good workaround for that in records. Of course, records can still implement interfaces and we could do some logical coupling using interfaces but it is not really the same as using inheritance. Additionally, from what I tested out SpringDoc doesn't really work well with picking up interface getter methods as fields of models, so as a result your API will end up having a model that looks like '{}' in SwaggerUI and I don't think this is what you would want.
Another possible workaround is using composition. This workaround is also not ideal since you will have some sort of nested parent object in your model and JSON as well.
So, if inheritance in your API is what you need, I'm afraid records are not the way to go and there is no workaround. Use classes, be it a default template provided by the generator out of the box or some other one that supports inheritance generation.
2. Records do not allow default values (there is a workaround though):
OpenAPI spec has a default: keyword which poses certain implementation challenges in records. It's not as straightforward as in classes and requires a certain workaround with a constructor. An example of that could be something like this:
This would technically work and although perhaps a bit messy, some could consider it still a good solution due to records advantage of immutability over classes.
But, remember that records are meant to be simple and immutable data carriers. It is kind of the core philosophy behind them. Of course, we can do stuff like this. But, perhaps at that point, you could be better off using just simple classes if default values are what you need in your API. A class solution like this looks much cleaner and more intuitive:
Generally, it all boils down to whether you want to sacrifice the simplicity of records and pollute them with this "default through constructor" approach but retain immutability, or go for a simpler class approach, although it potentially being more problematic due to its mutable nature
So far, due to the complexity of this workaround, I have not gotten to implement it in my template file. Although it seems like it is technically possible I will leave it as "an idea for future improvement" type of thing.
As mentioned by @grubeninspekteur, is indeed a tough case to consider.
Before we dive deeper into implementation details though, I feel like it is very important to mention and to fully understand how JsonNullable works. The underlying idea behind JsonNullable is that unlike Optional or just null it can represent 3 states:
1) Defined with a Non-Null Value:
This state occurs when JsonNullable wraps a non-null value. It represents a field that is explicitly set with a specific value. For instance, if you have a JSON field age and it is set to 30, JsonNullable would wrap this value as JsonNullable.of(30).
In code: JsonNullable<String> jsonValue = JsonNullable.of("Hello");
In JSON: { "jsonValue": "Hello" }
2) Defined with a Null Value:
In this state, JsonNullable explicitly wraps a null value. This is different from a field not being present at all; it's a clear indication that the field is set but its value is null. This is particularly important in APIs where setting a field to null might have specific implications, such as clearing a value.
In code: JsonNullable<String> jsonValue = JsonNullable.of(null);
In JSON: { "jsonValue": null }
3) Undefined:
The third state is when JsonNullable represents an undefined or non-existent field. This is used to indicate that a field is not included in the JSON payload at all. It's neither null nor set to any value. This state is crucial for partial updates in APIs, where the absence of a field means "leave unchanged", while null might mean "clear the value".
In code: JsonNullable<String> jsonValue = JsonNullable.undefined();
In JSON: {} (the field jsonValue does not appear in the JSON)
Here's a snipped from the documentation describing that:
The JsonNullable wrapper shall be used to wrap Java bean fields for which it is important to distinguish between an explicit "null" and the field not being present. A typical usage is when implementing Json Merge Patch where an explicit "null"has the meaning "set this field to null / remove this field" whereas a non-present field has the meaning "don't change the value of this field".
The default generation template approaches this 3-state scenario in a smart way. If the field is said to be nullable: in the OpenAPI spec, here's what get's generated:
This is a smart way to stick to this 3-way logic because:
If we NEVER call a fluent setter: the field remains as JsonNullable.undefined(). This sets isPresent to false and the underlying object is null. This means that the field doesn't even really exist and can be omitted from JSON
If we call a fluent setter with null : isPresent set to true. But the underlying object will be null.
If we call a fluent setter with some actual string text: the underlying String object will contain text supplied and isPresent will be set to true.
From what I understand, achieving the 3-states of JsonNullable in records is impossible..
In records, it's impossible to achieve the three distinct states of JsonNullable effectively. This limitation arises because you can't set a field's default value in a record to JsonNullable.undefined() directly. Moreover, using constructors to create default values typically involves null checks, which doesn't allow for distinguishing between JsonNullable being explicitly set to null, having a non-null value, or being undefined.
We are sacrificing one of the states with such construction trickery. In the first case, our jsonNullableValue can never be .of(null) and in the second case it can never be undefined().
So, if you want to be using JsonNullable you are better off using the default template or some other templates that are based on classes.
To conclude the limitations of using records as models for OpenAPI Springn generator:
inheritance can not be achieved
default values can be achieved but involve some not-very-pretty-looking constructor trickery
JsonNullable can not be implemented due to records' immutable nature and no default values for fields
Why Records could still be a reasonable approach for some APIs
At this point after reading through all of the limitations you might be wondering: "Why even try to use records at this point?"
Well, I want you to think about a few things in regard to your API:
Do I need inheritance in my API?
Do I need default values in the DTOs of my API?
Do I really need JsonNullable?
While the first two questions usually have clear answers based on your business requirements, the necessity of JsonNullable might not be as intuitive. As previously mentioned, JsonNullable is crucial for JSON Merge Patch to differentiate between three states. However, consider if JSON Merge Patch is necessary for your API. Alternative RESTful solutions for updating a resource include:
PUT for Full Updates: Supplying a full DTO in the request body to update every field.
PATCH with Full Body for Partial Updates: Submitting the whole body but changing only altered fields on the server side.
Field-Selective Updates/PATCH with Special DTO: Updating only the fields specified in a DTO containing a subset of fields.
Separate PATCH Request for Each Field: Suitable for highly granular updates on not overly large resources.
Query Parameter Updates: Ideal for simple scenarios with straightforward update logic.
Use of gRPC or GraphQL: These architectures often simplify handling partial updates.
If any of these alternatives align with your API's requirements, you might not need JsonNullable and could consider using records as DTOs, leveraging their immutability and reduced boilerplate for simpler API structures."
How to generate records:
1. template files:
In order to generate models/DTOs as records we will have to place 2 custom template files in our /resources/templates folder:
pojo.mustache -- this is an actual model generation template. In our case, it will be a template file responsible for generating records as models.
model.mustahce primarily includes imports and references pojo.mustache. In our case, this removes a few imports from the standard model template file and adds Lombok's @Builder annotation for the sake of convenience.
2. Known issues with the template (as of post date):
Functionally, there are no issues I have discovered yet, there are however a few aesthetic/formatting issues:
So far there is a little issue that bugs me a lot but that I can't wrap my head around yet and it is in the pojo.mustache template. It has to do purely with formatting. I can not figure out how to consistently have a single line break after each row. So far the generated records can have 0-2 rows in between fields which is a bit annoying and aesthetically unpleasing. If you know where the corresponding issue in the template is, please let me know.
I don't like that bean validation annotations like @Pattern or @Email or @Size or whatever are all generated into one line. Would like to have them generated each on a new line.
Those little issues I will try to get to someday later.
3. Plugin configuration:**
Make sure to include <templateDirectory>src/main/resources/templates</templateDirectory> and you're pretty much good to go
If you want to see a more detailed configuration of the plugin, feel free to look at the pom.xml file in my repository.
That's it!
Conclusions
To sum up, while records are not a one-size-fits-all solution for every API and use case, they can be quite useful, particularly for certain scenarios and simpler APIs, thanks to their immutable nature. This was the motivation behind my work on these templates. As always, I encourage you to consider your specific needs, feel free to experiment, and don't hesitate to reach out with any questions about the templates I've created.
If you think I've overlooked certain limitations or made an error in this discussion, please let me know—let's make this a collaborative effort.
For future improvements, I'm considering exploring the implementation of default values through constructors, as it seems technically feasible, as well as trying to figure out line formatting issues. Your input or interest in this feature would be greatly appreciated, so do let me know if this is something you'd find useful.
I apologize for the length of this comment, but I hope it proves helpful in your decision-making process regarding the use of records in API development. Thank you for reading, and I look forward to your thoughts and contributions!
Reacted by hgjd, Pankaj PATIL, Dmitriy Belyaev, Cristian A. Ontivero, Floris Kraak, Ruslan Stelmachenko, Gerard Bosch, Fischblick, rafal-osiecki, JY Cr and 1 more
Reacted by Tim Feuerbach, Pankaj PATIL, Eric Nowak, racing-rissole, JY Cr, Floris Kraak, Ruslan Stelmachenko, Alexandre Xavier, Gerard Bosch, Boubker Boulahdid and 7 more
Another reason that Java Records would not fully support json schema object payloads is that they do not support properties where invalid java names are used. If one built the model by implementing the Map interface, those invalidly named keys would be preserved and one could write getters for them.
That's what I do in my Java implementation.
... for production-ready templates which generates Java records! It is continuously being updated to support more and more configurations. It supports the latest 13 releases of openapi-generator 😊
Reacted by Carl-Erik Kopseng, Boubker Boulahdid and Wojtek
For anyone looking for Java record support, you may also want to try hurdy-gurdy, an OpenAPI code generator that we maintain.
Unlike openapi-generator, which supports many languages and relies primarily on templates, hurdy-gurdy focuses specifically on Java and Kotlin and generates source code programmatically using JavaPoet and KotlinPoet. This makes the generator easier to evolve while producing more idiomatic Java and Kotlin code.
Because both the generator and generated output run entirely within the JVM ecosystem, the output is thoroughly tested for compilability, through Jackson serialization/deserialization round trips, and more.
hurdy-gurdy provides Maven and Gradle plugins, as well as a CLI tool. It has Spring and Quarkus generation modes. For Java DTOs, it can already generate regular POJOs, Lombok-based classes, or Java records.
Reacted by Pasha Finkelshteyn and Andreas Maechler
It's not included into your project, why would you care? It's just a cli tool/build system plugin, not an integral part of your project, you don't even link with it, so you don't have to disclose the source code of anything but your modifications to the tool/plugin themselves.
@Chrimle hurdy-gurdy 3.2 is re-licensed under Apache 2.0, in line with other open source projects from /courseorchestra.
However, @asm0dey was right -- your license requirement must be about run-time dependencies, not build tools, or you shouldn't have been using Java compiler in the first place 🤷♂️
I've started on this for the Spring generator as an opt-in useRecords, in small PRs so each one is easy to review. #25200 covers plain models: validation, defaults and JsonNullable, where absent, null and value all work. Next come builders, oneOf with sealed interfaces, additionalProperties, then allOf, whose design I'll post here first.
public record DossierDto(
String language,
Boolean active
) {
public DossierDto {
if (language == null) {
language = "fr-BE";
}
if (active == null) {
active = Boolean.TRUE;
}
}
}
@jpfinne yes, that is what #25200 generates: the compact constructor gives a null component the default the class's field has (schema default, empty container, JsonNullable.undefined()). One consequence: an explicit null also becomes the default, because a record cannot tell null from absent.
@jpfinne that works from Jackson 2.19, but the default Spring Boot 3 pom brings 2.17.3, where reading a record with an @JsonUnwrapped component fails ("combination not yet supported"). What works on both: the discriminator parent as an interface (sealed with useSealed) and each child a flat record with all properties, as kotlin-spring does. That is my plan for allOf.
allOf is next, as planned above: a discriminator parent becomes an interface (sealed with useSealed) that keeps the @JsonTypeInfo block, and each child a flat record with all properties. Other allOf models are already flat, so already records. Multi-level chains and redeclared properties stay classes for now, with the reason logged.
Records for the Spring generator shipped in 7.27.0: useRecords=true (Spring Boot 3 or 4) generates models as Java records, and a model that cannot be one yet stays a class with the reason logged. allOf families come next in #25492. For the other Java generators, the question is open in #22175.
For other java generators, it would be nice to avoid code duplication. The record implementation could be moved to AbstractJavaCodegen, CodegenConstants...
Activity
spacether commentedon Sep 28, 2021
JEP 39 is defined here:
https://openjdk.java.net/jeps/395
hgjd commentedon Feb 23, 2023
It could even be a sensible default when using the Spring generator with
useSpringBoot3, as that requires Java 17.The model cannot be an enum or extend another class. Am I missing any other limitations?
fabiofranco85 commentedon May 19, 2023
It would be amazing if we got this improvement - specially when we set
useSpringBoot3One question though is: will the records DTOs cope well with swagger annotations?
Kinae commentedon May 31, 2023
Plus one on this
Aryesia commentedon Jun 2, 2023
Plus one
grubeninspekteur commentedon Jun 2, 2023
Please don't write "plus one", it clutters the issue and generates notifications for subscribers. You can "thumbs up" the initial description.
I would really like to help implementing this feature, but I am overwhelmed after looking into the pojo mustache template. There seem to be so many special cases to consider (Jackson nullables for example).
GlobeDaBoarder commentedon Dec 18, 2023
Intro
So, this issue caught my attention a few months ago. And recently I started working on setting up a new project with OpenAPI generator and decided to give generating records as models a try.
I'm excited to share that this endeavor has been largely successful, and our project now leverages the immutable nature of records for its DTOs.
I'm here to share my findings, solutions, and templates with you. It's important to note, however, that while this approach has worked well for us, it might not be universally suitable, particularly due to some inherent limitations of records. In my experience, this strategy shows the most promise for simpler APIs, where the benefits of records truly shine.
Before diving into the solutions and templates, let's first unpack the limitations of records. Understanding these constraints is crucial for evaluating whether records are the right fit for your API development needs
Limitations:
1. Records do not allow inheritance:
OpenAPI spec allows inheritance in the API and so does the default template used by openapi generator for spring for example. Since records are technically a very special type of class that is final and immutable, it also extends the
java. lang. Recordclass. Due to Java's nature of "single inheritance" (it can extend only one class), creating inheritance models with records can not be achieved.I believe there is no good workaround for that in records. Of course, records can still implement interfaces and we could do some logical coupling using interfaces but it is not really the same as using inheritance. Additionally, from what I tested out SpringDoc doesn't really work well with picking up interface getter methods as fields of models, so as a result your API will end up having a model that looks like
'{}'in SwaggerUI and I don't think this is what you would want.Another possible workaround is using composition. This workaround is also not ideal since you will have some sort of nested parent object in your model and JSON as well.
So, if inheritance in your API is what you need, I'm afraid records are not the way to go and there is no workaround. Use classes, be it a default template provided by the generator out of the box or some other one that supports inheritance generation.
2. Records do not allow default values (there is a workaround though):
OpenAPI spec has a
default:keyword which poses certain implementation challenges in records. It's not as straightforward as in classes and requires a certain workaround with a constructor. An example of that could be something like this:This would technically work and although perhaps a bit messy, some could consider it still a good solution due to records advantage of immutability over classes.
But, remember that records are meant to be simple and immutable data carriers. It is kind of the core philosophy behind them. Of course, we can do stuff like this. But, perhaps at that point, you could be better off using just simple classes if default values are what you need in your API. A class solution like this looks much cleaner and more intuitive:
Generally, it all boils down to whether you want to sacrifice the simplicity of records and pollute them with this "default through constructor" approach but retain immutability, or go for a simpler class approach, although it potentially being more problematic due to its mutable nature
So far, due to the complexity of this workaround, I have not gotten to implement it in my template file. Although it seems like it is technically possible I will leave it as "an idea for future improvement" type of thing.
3. JsonNullable (jackson-databind-nullable) wrapper:
As mentioned by @grubeninspekteur, is indeed a tough case to consider.
Before we dive deeper into implementation details though, I feel like it is very important to mention and to fully understand how JsonNullable works. The underlying idea behind JsonNullable is that unlike
Optionalor justnullit can represent 3 states:1) Defined with a Non-Null Value:
JsonNullablewraps a non-null value. It represents a field that is explicitly set with a specific value. For instance, if you have a JSON field age and it is set to 30,JsonNullablewould wrap this value asJsonNullable.of(30).JsonNullable<String> jsonValue = JsonNullable.of("Hello");{ "jsonValue": "Hello" }2) Defined with a Null Value:
JsonNullableexplicitly wraps anullvalue. This is different from a field not being present at all; it's a clear indication that the field is set but its value is null. This is particularly important in APIs where setting a field tonullmight have specific implications, such as clearing a value.JsonNullable<String> jsonValue = JsonNullable.of(null);In JSON:
{ "jsonValue": null }3) Undefined:
JsonNullable<String> jsonValue = JsonNullable.undefined();{} (the field jsonValue does not appear in the JSON)Here's a snipped from the documentation describing that:
The default generation template approaches this 3-state scenario in a smart way. If the field is said to be
nullable:in the OpenAPI spec, here's what get's generated:Field:
Fluent setter:
Getter:
This is a smart way to stick to this 3-way logic because:
isPresenttofalseand the underlying object isnull. This means that the field doesn't even really exist and can be omitted from JSONisPresentset totrue. But the underlying object will benull.Stringobject will contain text supplied andisPresentwill be set totrue.From what I understand, achieving the 3-states of
JsonNullablein records is impossible..In records, it's impossible to achieve the three distinct states of JsonNullable effectively. This limitation arises because you can't set a field's default value in a record to JsonNullable.undefined() directly. Moreover, using constructors to create default values typically involves null checks, which doesn't allow for distinguishing between JsonNullable being explicitly set to null, having a non-null value, or being undefined.
If we try something like this:
Or this:
We are sacrificing one of the states with such construction trickery. In the first case, our
jsonNullableValuecan never be.of(null)and in the second case it can never beundefined().So, if you want to be using
JsonNullableyou are better off using the default template or some other templates that are based on classes.To conclude the limitations of using records as models for OpenAPI Springn generator:
Why Records could still be a reasonable approach for some APIs
At this point after reading through all of the limitations you might be wondering: "Why even try to use records at this point?"
Well, I want you to think about a few things in regard to your API:
JsonNullable?While the first two questions usually have clear answers based on your business requirements, the necessity of JsonNullable might not be as intuitive. As previously mentioned,
JsonNullableis crucial for JSON Merge Patch to differentiate between three states. However, consider if JSON Merge Patch is necessary for your API. Alternative RESTful solutions for updating a resource include:If any of these alternatives align with your API's requirements, you might not need
JsonNullableand could consider using records as DTOs, leveraging their immutability and reduced boilerplate for simpler API structures."How to generate records:
1. template files:
In order to generate models/DTOs as records we will have to place 2 custom template files in our
/resources/templatesfolder:pojo.mustache-- this is an actual model generation template. In our case, it will be a template file responsible for generating records as models.You can also find it here, in my little demo GitHub repo I made
model.mustahceprimarily includes imports and referencespojo.mustache. In our case, this removes a few imports from the standard model template file and adds Lombok's@Builderannotation for the sake of convenience.You can find this one here:
2. Known issues with the template (as of post date):
Functionally, there are no issues I have discovered yet, there are however a few aesthetic/formatting issues:
pojo.mustachetemplate. It has to do purely with formatting. I can not figure out how to consistently have a single line break after each row. So far the generated records can have 0-2 rows in between fields which is a bit annoying and aesthetically unpleasing. If you know where the corresponding issue in the template is, please let me know.@Patternor@Emailor@Sizeor whatever are all generated into one line. Would like to have them generated each on a new line.Those little issues I will try to get to someday later.
3. Plugin configuration:**
Make sure to include
<templateDirectory>src/main/resources/templates</templateDirectory>and you're pretty much good to goIf you want to see a more detailed configuration of the plugin, feel free to look at the
pom.xmlfile in my repository.That's it!
Conclusions
To sum up, while records are not a one-size-fits-all solution for every API and use case, they can be quite useful, particularly for certain scenarios and simpler APIs, thanks to their immutable nature. This was the motivation behind my work on these templates. As always, I encourage you to consider your specific needs, feel free to experiment, and don't hesitate to reach out with any questions about the templates I've created.
If you think I've overlooked certain limitations or made an error in this discussion, please let me know—let's make this a collaborative effort.
For future improvements, I'm considering exploring the implementation of default values through constructors, as it seems technically feasible, as well as trying to figure out line formatting issues. Your input or interest in this feature would be greatly appreciated, so do let me know if this is something you'd find useful.
I apologize for the length of this comment, but I hope it proves helpful in your decision-making process regarding the use of records in API development. Thank you for reading, and I look forward to your thoughts and contributions!
spacether commentedon Dec 18, 2023
Another reason that Java Records would not fully support json schema object payloads is that they do not support properties where invalid java names are used. If one built the model by implementing the Map interface, those invalidly named keys would be preserved and one could write getters for them.
That's what I do in my Java implementation.
Chrimle commentedon Sep 8, 2024
This is a matter of overriding
.mustachetemplate files to generate Java records instead of Java classes.Check out openapi-to-java-records-mustache-templates
... for production-ready templates which generates Java records! It is continuously being updated to support more and more configurations. It supports the latest 13 releases of openapi-generator 😊
lmebonia commentedon Aug 6, 2025
Plus one
datoodvali commentedon Aug 6, 2025
+1
TeyAbsolut commentedon Jan 23, 2026
+1 in 2026
bhuism commentedon Jul 13, 2026
+1
inponomarev commentedon Jul 18, 2026
For anyone looking for Java record support, you may also want to try
hurdy-gurdy, an OpenAPI code generator that we maintain.Unlike
openapi-generator, which supports many languages and relies primarily on templates,hurdy-gurdyfocuses specifically on Java and Kotlin and generates source code programmatically using JavaPoet and KotlinPoet. This makes the generator easier to evolve while producing more idiomatic Java and Kotlin code.Because both the generator and generated output run entirely within the JVM ecosystem, the output is thoroughly tested for compilability, through Jackson serialization/deserialization round trips, and more.
hurdy-gurdyprovides Maven and Gradle plugins, as well as a CLI tool. It has Spring and Quarkus generation modes. For Java DTOs, it can already generate regular POJOs, Lombok-based classes, or Java records.1 remaining item
Chrimle commentedon Jul 18, 2026
@asm0dey Apache 2.0 License is a bare minimum in my book.
asm0dey commentedon Jul 18, 2026
Could you tell me more, please?
It's not included into your project, why would you care? It's just a cli tool/build system plugin, not an integral part of your project, you don't even link with it, so you don't have to disclose the source code of anything but your modifications to the tool/plugin themselves.
inponomarev commentedon Jul 19, 2026
@Chrimle hurdy-gurdy 3.2 is re-licensed under Apache 2.0, in line with other open source projects from /courseorchestra.
However, @asm0dey was right -- your license requirement must be about run-time dependencies, not build tools, or you shouldn't have been using Java compiler in the first place 🤷♂️
Chrimle commentedon Jul 19, 2026
On second thought, keep your
herpy-derpy, no longer interested.asm0dey commentedon Jul 19, 2026
No need to be rude, @Chrimle. Java is GPL-licensed, so I'm not sure how you use it
Chrimle commentedon Jul 19, 2026
I find it rude to promote a competing project, then ping me personally for whatever reason...
Sekator778 commentedon Oct 9, 2026
I've started on this for the Spring generator as an opt-in
useRecords, in small PRs so each one is easy to review. #25200 covers plain models: validation, defaults andJsonNullable, where absent, null and value all work. Next come builders, oneOf with sealed interfaces,additionalProperties, then allOf, whose design I'll post here first.jpfinne commentedon Oct 9, 2026
@Sekator778 @GlobeDaBoarder we can have default values in records using compact constructors:
Sekator778 commentedon Oct 9, 2026
@jpfinne yes, that is what #25200 generates: the compact constructor gives a null component the default the class's field has (schema default, empty container,
JsonNullable.undefined()). One consequence: an explicit null also becomes the default, because a record cannot tell null from absent.jpfinne commentedon Oct 9, 2026
@GlobeDaBoarder I've tested JsonNullable with Spring-boot-4 and the latest JsonNullable implementation.
I thing record are well supported.
I've tested it with
and jackson3
JsonMapper.builder().build()The deserialization and serialization just works as for classes.
So what is is your concern about JsonNullable?
jpfinne commentedon Oct 9, 2026
For inheritance, I think it is doable using composition or delegation using
@JsonUnwrappedThis contract would generated inheritance with classes:
Using record and JsonUwrapped:
It works with serialization and deserialization.
Sekator778 commentedon Oct 10, 2026
@jpfinne that works from Jackson 2.19, but the default Spring Boot 3 pom brings 2.17.3, where reading a record with an
@JsonUnwrappedcomponent fails ("combination not yet supported"). What works on both: the discriminator parent as an interface (sealed withuseSealed) and each child a flat record with all properties, as kotlin-spring does. That is my plan for allOf.Sekator778 commentedon Oct 31, 2026
allOf is next, as planned above: a discriminator parent becomes an interface (sealed with
useSealed) that keeps the@JsonTypeInfoblock, and each child a flat record with all properties. Other allOf models are already flat, so already records. Multi-level chains and redeclared properties stay classes for now, with the reason logged.Sekator778 commentedon Nov 7, 2026
Records for the Spring generator shipped in 7.27.0:
useRecords=true(Spring Boot 3 or 4) generates models as Java records, and a model that cannot be one yet stays a class with the reason logged. allOf families come next in #25492. For the other Java generators, the question is open in #22175.jpfinne commentedon Nov 7, 2026
For other java generators, it would be nice to avoid code duplication. The record implementation could be moved to AbstractJavaCodegen, CodegenConstants...