Skip to content

Add support to build JsonItemReader when no Resource is provided #3739

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
wants to merge 1 commit into from
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 @@ -73,6 +73,9 @@ public JsonItemReader(Resource resource, JsonObjectReader<T> jsonObjectReader) {
this.jsonObjectReader = jsonObjectReader;
setExecutionContextName(ClassUtils.getShortName(JsonItemReader.class));
}
public JsonItemReader(){
setExecutionContextName(ClassUtils.getShortName(JsonItemReader.class));
}

/**
* Set the {@link JsonObjectReader} to use to read and map Json fragments to domain objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ public JsonItemReaderBuilder<T> currentItemCount(int currentItemCount) {
*/
public JsonItemReader<T> build() {
Assert.notNull(this.jsonObjectReader, "A json object reader is required.");
Assert.notNull(this.resource, "A resource is required.");
if (this.saveState) {
Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true.");
}

JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader);
JsonItemReader<T> reader = new JsonItemReader<>();
reader.setResource(this.resource);
reader.setJsonObjectReader(this.jsonObjectReader);
reader.setName(this.name);
reader.setStrict(this.strict);
reader.setSaveState(this.saveState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ public void testValidation() {
iae.getMessage());
}

try {
new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
.build();
fail("A resource is required.");
}
catch (IllegalArgumentException iae) {
assertEquals("A resource is required.",
iae.getMessage());
}

try {
new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
Expand Down Expand Up @@ -102,4 +91,24 @@ public void testConfiguration() {
Object executionContext = getField(itemReader, "executionContextUserSupport");
Assert.assertEquals("jsonItemReader", getField(executionContext, "name"));
}
@Test
public void shouldBuildJsonItemReaderWhenResourceIsNotProvided(){
JsonItemReader<String> itemReader = new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
.saveState(true)
.strict(true)
.name("jsonItemReader")
.maxItemCount(100)
.currentItemCount(50)
.build();

Assert.assertEquals(this.jsonObjectReader, getField(itemReader, "jsonObjectReader"));
Assert.assertEquals(100, getField(itemReader, "maxItemCount"));
Assert.assertEquals(50, getField(itemReader, "currentItemCount"));
Assert.assertTrue((Boolean) getField(itemReader, "saveState"));
Assert.assertTrue((Boolean) getField(itemReader, "strict"));
Object executionContext = getField(itemReader, "executionContextUserSupport");
Assert.assertEquals("jsonItemReader", getField(executionContext, "name"));

}
}