Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions options.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ The names used for generated methods, classes, etc. can be changed via the follo
| `@RecordBuilder.Options(fileIndent = " ")` | Return the file indent to use. |
| `@RecordBuilder.Options(prefixEnclosingClassNames = true/false)` | If the record is declared inside another class, the outer class's name will be prefixed to the builder name if this returns true. The default is `true`. |

## Jackson Support

| option | details |
|--------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `@RecordBuilder.Options(addJacksonAnnotations = true/false)` | If true, builders will be annotated with `@JsonPOJOBuilder` Jackson annotations which can be used in combination with `@JsonDeserialize(builder = ...)`. See [TestJacksonAnnotations](./record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestJacksonAnnotations.java) for an example. The default is `false`. |

## Miscellaneous

| option | details |
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<hibernate-validator-version>6.2.0.Final</hibernate-validator-version>
<jakarta-validation-api-version>3.1.0</jakarta-validation-api-version>
<javax-el-version>3.0.1-b09</javax-el-version>
<jackson-version>2.19.0</jackson-version>
<central-publishing-maven-plugin-version>0.7.0</central-publishing-maven-plugin-version>
</properties>

Expand Down Expand Up @@ -165,6 +166,12 @@
<version>${hibernate-validator-version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@
* @see #nullablePattern
*/
boolean defaultNotNull() default false;

boolean addJacksonAnnotations() default false;
}

@Retention(RetentionPolicy.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class InternalRecordBuilderProcessor {
builder.addAnnotation(recordBuilderGeneratedAnnotation);
}

addJacksonAnnotations();

if (!validateMethodNameConflicts(processingEnv, recordFacade.element())) {
builderType = Optional.empty();
return;
Expand Down Expand Up @@ -196,6 +198,18 @@ private void addVisibility(boolean builderIsInRecordPackage, Set<Modifier> modif
}
}

private void addJacksonAnnotations() {
if (!metaData.addJacksonAnnotations()) {
return;
}

final var annotationSpec = AnnotationSpec
Copy link

@semyon-levin-workato semyon-levin-workato Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid it's not that simple. Recently, Jackson 3 was released with changed package names. Is it possible to check what annotations are presented on the classpath and add appropriate annotation here?

.builder(ClassName.get("com.fasterxml.jackson.databind.annotation", "JsonPOJOBuilder"))
.addMember("withPrefix", "$S", metaData.setterPrefix()).build();

builder.addAnnotation(annotationSpec);
}

private void addOnceOnlySupport() {
if (recordComponents.isEmpty()) {
return;
Expand Down
5 changes: 5 additions & 0 deletions record-builder-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 The original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.soabase.recordbuilder.test;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.soabase.recordbuilder.core.RecordBuilder;

import java.util.Map;

public interface JacksonAnnotated {
String name();

String type();

Map<String, Object> properties();

@RecordBuilder
@RecordBuilder.Options(addJacksonAnnotations = true, useImmutableCollections = true, prefixEnclosingClassNames = false)
@JsonDeserialize(builder = JacksonAnnotatedRecordBuilder.class)
record JacksonAnnotatedRecord(String name, @RecordBuilder.Initializer("DEFAULT_TYPE") String type,
Map<String, Object> properties) implements JacksonAnnotated {
public static final String DEFAULT_TYPE = "dummy";
}

@RecordBuilder
@RecordBuilder.Options(addJacksonAnnotations = true, useImmutableCollections = true, prefixEnclosingClassNames = false, setterPrefix = "set")
@JsonDeserialize(builder = JacksonAnnotatedRecordCustomSetterPrefixBuilder.class)
record JacksonAnnotatedRecordCustomSetterPrefix(String name, @RecordBuilder.Initializer("DEFAULT_TYPE") String type,
Map<String, Object> properties) implements JacksonAnnotated {
public static final String DEFAULT_TYPE = "dummy";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2019 The original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.soabase.recordbuilder.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import io.soabase.recordbuilder.test.JacksonAnnotated.JacksonAnnotatedRecord;
import io.soabase.recordbuilder.test.JacksonAnnotated.JacksonAnnotatedRecordCustomSetterPrefix;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class TestJacksonAnnotations {
private final ObjectMapper objectMapper = new ObjectMapper();

@ParameterizedTest
@MethodSource("recordBuilders")
void addsJsonPOJOBuilderAnnotation(Class<? extends JacksonAnnotated> type, String expectedPrefix) {
final var annotations = Arrays.stream(type.getAnnotations()).toList();
assertThat(annotations).filteredOn(annotation -> annotation.annotationType().equals(JsonPOJOBuilder.class))
.hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(JsonPOJOBuilder.class))
.satisfies(annotation -> {
assertThat(annotation.withPrefix()).isEqualTo(expectedPrefix);
});
}

static Stream<Arguments> recordBuilders() {
return Stream.of(arguments(JacksonAnnotatedRecordBuilder.class, ""),
arguments(JacksonAnnotatedRecordCustomSetterPrefixBuilder.class, "set"));
}

@ParameterizedTest
@ValueSource(classes = { JacksonAnnotatedRecord.class, JacksonAnnotatedRecordCustomSetterPrefix.class })
void deserializingModelInvokesBuilder(Class<? extends JacksonAnnotated> type) throws JsonProcessingException {
final var json = """
{
"name" : "test"
}
""";

final var model = objectMapper.readValue(json, type);
assertThat(model.name()).isEqualTo("test");
assertThat(model.type()).isEqualTo("dummy"); // default value
assertThat(model.properties()).isNotNull().isEmpty(); // non-null initialized immutable collection
}
}