Skip to content

Add default component name in constructor of AvroItemReader #4285

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

Closed
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 @@ -35,13 +35,15 @@
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* An {@link ItemReader} that deserializes data from a {@link Resource} containing
* serialized Avro objects.
*
* @author David Turanski
* @author Mahmoud Ben Hassine
* @author Song JaeGeun
* @since 4.2
*/
public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> {
Expand All @@ -61,6 +63,8 @@ public class AvroItemReader<T> extends AbstractItemCountingItemStreamItemReader<
* @param clazz the data type to be deserialized.
*/
public AvroItemReader(Resource resource, Class<T> clazz) {
setName(ClassUtils.getShortName(AvroItemReader.class));

Assert.notNull(resource, "'resource' is required.");
Assert.notNull(clazz, "'class' is required.");

Expand All @@ -78,6 +82,8 @@ public AvroItemReader(Resource resource, Class<T> clazz) {
* @param schema the {@link Resource} containing the Avro schema.
*/
public AvroItemReader(Resource data, Resource schema) {
setName(ClassUtils.getShortName(AvroItemReader.class));

Assert.notNull(data, "'data' is required.");
Assert.state(data.exists(), "'data' " + data.getFilename() + " does not exist.");
Assert.notNull(schema, "'schema' is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* A builder implementation for the {@link AvroItemReader}.
*
* @author David Turanski
* @author Mahmoud Ben Hassine
* @author Song JaeGeun
* @since 4.2
*/
public class AvroItemReaderBuilder<T> {
Expand Down Expand Up @@ -170,10 +170,6 @@ public AvroItemReader<T> build() {

avroItemReader.setSaveState(this.saveState);

if (this.saveState) {
Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true.");
}

Comment on lines -173 to -176
Copy link
Contributor

Choose a reason for hiding this comment

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

remove useless assert state

This assertion is not useless. Here is a case where the name could end up being null or empty even with a default name in the constructor:

AvroItemReader avroItemReader = new AvroItemReaderBuilder()
		.name(getNameFromPropertiesFile())
		// set other properties
		.build();

In this example, if getNameFromPropertiesFile() returns null or an empty string, the reader will be created in an inconsistent state. That's why it is better to fail fast at the builder level.

avroItemReader.setName(this.name);
avroItemReader.setCurrentItemCount(this.currentItemCount);
avroItemReader.setMaxItemCount(this.maxItemCount);
Expand Down