Skip to content

Commit

Permalink
fix #394: ensures that defaults are copied
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and iocanel committed Jul 3, 2023
1 parent dd7451f commit 27b9f29
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ private static List<Statement> 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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 27b9f29

Please sign in to comment.