Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty primitive parameters in DefaultConstructorBuilder #568

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ public void add(Object value) {

@Override
public void addEmptyParameter() {
this.values.add(null);
Constructor<?> constructor = ((DefaultConstructorMetadata) this.metadata).constructor();
Class<?> type = constructor.getParameterTypes()[this.values.size()];
if(boolean.class.equals(type)) {
this.values.add(Boolean.FALSE);
} else if(char.class.equals(type)) {
this.values.add(Character.valueOf((char)0));
} else if(type.isPrimitive()) {
this.values.add(Byte.valueOf((byte)0));
} else {
this.values.add(null);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ClassGraphClassScannerTest {
void shouldReturnEntities() {
Set<Class<?>> entities = classScanner.entities();
Assertions.assertNotNull(entities);
assertThat(entities).hasSize(31)
assertThat(entities).hasSize(32)
.contains(Person.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.reflection.entities.constructor.BookUser;
import org.eclipse.jnosql.mapping.reflection.entities.constructor.Counter;
import org.jboss.weld.junit5.auto.AddPackages;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -34,12 +35,16 @@
class DefaultConstructorBuilderTest {

private ConstructorMetadata constructor;
private ConstructorMetadata counterConstructor;

@BeforeEach
void setUp(){
ClassConverter converter = new ReflectionClassConverter();
EntityMetadata entityMetadata = converter.apply(BookUser.class);
this.constructor = entityMetadata.constructor();

EntityMetadata counterMetadata = converter.apply(Counter.class);
this.counterConstructor = counterMetadata.constructor();
}

@Test
Expand Down Expand Up @@ -99,4 +104,18 @@ void shouldEqualsHashCode(){
assertThat(builder).isEqualTo(other);
assertThat(builder).hasSameHashCodeAs(other);
}

@Test
void shouldHandleEmptyPrimitives() {
ConstructorBuilder builder = DefaultConstructorBuilder.of(counterConstructor);
builder.addEmptyParameter();
builder.addEmptyParameter();
builder.addEmptyParameter();
builder.addEmptyParameter();
Counter counter = builder.build();
assertThat(counter.count()).isEqualTo(0);
assertThat(counter.active()).isFalse();
assertThat(counter.ratio()).isEqualTo(0d);
assertThat(counter.code()).isEqualTo((char)0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Jesse Gallagher
*/
package org.eclipse.jnosql.mapping.reflection.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;

@Entity
public record Counter(@Column int count, @Column boolean active, @Column double ratio, @Column char code) {

}
Loading