From 27b9f29f7e238db9c35a3d31c167ac60778a8719 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Fri, 23 Jun 2023 09:13:09 -0400 Subject: [PATCH] fix #394: ensures that defaults are copied --- .../builder/internal/functions/ClazzAs.java | 4 ++ .../java/io/sundr/tests/WithDefaults.java | 38 +++++++++++++++++++ .../java/io/sundr/tests/WithDefaultsTest.java | 32 ++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/buildable-fields/src/main/java/io/sundr/tests/WithDefaults.java create mode 100644 tests/buildable-fields/src/test/java/io/sundr/tests/WithDefaultsTest.java diff --git a/annotations/builder/src/main/java/io/sundr/builder/internal/functions/ClazzAs.java b/annotations/builder/src/main/java/io/sundr/builder/internal/functions/ClazzAs.java index 41e8c3746..230230941 100644 --- a/annotations/builder/src/main/java/io/sundr/builder/internal/functions/ClazzAs.java +++ b/annotations/builder/src/main/java/io/sundr/builder/internal/functions/ClazzAs.java @@ -619,6 +619,10 @@ private static List toInstanceConstructorBody(TypeDef clazz, TypeDef statements.add(new StringStatement("this.fluent = " + fluent + "; ")); } + if (hasDefaultConstructor(clazz)) { + statements.add(new StringStatement("instance = (instance != null ? instance : new " + clazz.getName() + "());\n")); + } + StringBuilder builder = new StringBuilder("if (instance != null) {\n"); for (Property property : constructor.getArguments()) { diff --git a/tests/buildable-fields/src/main/java/io/sundr/tests/WithDefaults.java b/tests/buildable-fields/src/main/java/io/sundr/tests/WithDefaults.java new file mode 100644 index 000000000..51107f896 --- /dev/null +++ b/tests/buildable-fields/src/main/java/io/sundr/tests/WithDefaults.java @@ -0,0 +1,38 @@ +/** + * Copyright 2015 The original 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.sundr.tests; + +import io.sundr.builder.annotations.Buildable; + +/** + * A simple Pojo that demonstrates default fields are preserved + */ +@Buildable +public class WithDefaults { + + private String field = "Hello"; + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + +} diff --git a/tests/buildable-fields/src/test/java/io/sundr/tests/WithDefaultsTest.java b/tests/buildable-fields/src/test/java/io/sundr/tests/WithDefaultsTest.java new file mode 100644 index 000000000..aadd3ba6b --- /dev/null +++ b/tests/buildable-fields/src/test/java/io/sundr/tests/WithDefaultsTest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2015 The original 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.sundr.tests; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class WithDefaultsTest { + + @Test + public void testGeneratedBuilder() { + WithDefaults withDefaults = new WithDefaultsBuilder((WithDefaults) null).build(); + assertEquals("Hello", withDefaults.getField()); + } + +}