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

Using Optional.ofNullable() at the fluent setters to prevent NPE #20406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -151,7 +151,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{! begin feature: fluent setter methods }}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
{{#openApiNullable}}
this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.of({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}};
this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.ofNullable({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}};
{{/openApiNullable}}
{{^openApiNullable}}
this.{{name}} = {{name}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void doAnnotateDatesOnModelParametersWithOptionalAndJsonNullable() throws
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"))
.toProperty().toType()
.assertMethod("born", "LocalDate")
.bodyContainsLines("this.born = Optional.of(born)")
.bodyContainsLines("this.born = Optional.ofNullable(born)")
.doesNotHaveComment();
}

Expand Down Expand Up @@ -4397,9 +4397,10 @@ private void assertJsonNullableMethod(JavaFileAssert javaFileAssert, String type

private void assertWrapperMethod(JavaFileAssert javaFileAssert, String wrapperType, String type, String expectedName, String getterReturnType){
String methodName = StringUtils.capitalize(expectedName);
var of = wrapperType.equals("Optional") ? "ofNullable" : "of";
javaFileAssert.assertMethod(expectedName)
.hasReturnType("Animal")
.bodyContainsLines("this."+expectedName+" = "+wrapperType+".of("+expectedName+");", "return this;")
.bodyContainsLines("this." + expectedName + " = "+wrapperType+ "." + of + "(" +expectedName+");", "return this;")
.assertParameter(expectedName)
.hasType(type)
.toMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Category {
private Optional<@Pattern(regexp = "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$") String> name = Optional.empty();

public Category id(Long id) {
this.id = Optional.of(id);
this.id = Optional.ofNullable(id);
return this;
}

Expand All @@ -45,7 +45,7 @@ public void setId(Optional<Long> id) {
}

public Category name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ModelApiResponse {
private Optional<String> message = Optional.empty();

public ModelApiResponse code(Integer code) {
this.code = Optional.of(code);
this.code = Optional.ofNullable(code);
return this;
}

Expand All @@ -49,7 +49,7 @@ public void setCode(Optional<Integer> code) {
}

public ModelApiResponse type(String type) {
this.type = Optional.of(type);
this.type = Optional.ofNullable(type);
return this;
}

Expand All @@ -68,7 +68,7 @@ public void setType(Optional<String> type) {
}

public ModelApiResponse message(String message) {
this.message = Optional.of(message);
this.message = Optional.ofNullable(message);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static StatusEnum fromValue(String value) {
private Optional<Boolean> complete = Optional.of(false);

public Order id(Long id) {
this.id = Optional.of(id);
this.id = Optional.ofNullable(id);
return this;
}

Expand All @@ -94,7 +94,7 @@ public void setId(Optional<Long> id) {
}

public Order petId(Long petId) {
this.petId = Optional.of(petId);
this.petId = Optional.ofNullable(petId);
return this;
}

Expand All @@ -113,7 +113,7 @@ public void setPetId(Optional<Long> petId) {
}

public Order quantity(Integer quantity) {
this.quantity = Optional.of(quantity);
this.quantity = Optional.ofNullable(quantity);
return this;
}

Expand All @@ -132,7 +132,7 @@ public void setQuantity(Optional<Integer> quantity) {
}

public Order shipDate(OffsetDateTime shipDate) {
this.shipDate = Optional.of(shipDate);
this.shipDate = Optional.ofNullable(shipDate);
return this;
}

Expand All @@ -151,7 +151,7 @@ public void setShipDate(Optional<OffsetDateTime> shipDate) {
}

public Order status(StatusEnum status) {
this.status = Optional.of(status);
this.status = Optional.ofNullable(status);
return this;
}

Expand All @@ -170,7 +170,7 @@ public void setStatus(Optional<StatusEnum> status) {
}

public Order complete(Boolean complete) {
this.complete = Optional.of(complete);
this.complete = Optional.ofNullable(complete);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Pet(String name, List<String> photoUrls) {
}

public Pet id(Long id) {
this.id = Optional.of(id);
this.id = Optional.ofNullable(id);
return this;
}

Expand All @@ -111,7 +111,7 @@ public void setId(Optional<Long> id) {
}

public Pet category(Category category) {
this.category = Optional.of(category);
this.category = Optional.ofNullable(category);
return this;
}

Expand Down Expand Up @@ -203,7 +203,7 @@ public void setTags(List<@Valid Tag> tags) {
}

public Pet status(StatusEnum status) {
this.status = Optional.of(status);
this.status = Optional.ofNullable(status);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Tag {
private Optional<String> name = Optional.empty();

public Tag id(Long id) {
this.id = Optional.of(id);
this.id = Optional.ofNullable(id);
return this;
}

Expand All @@ -45,7 +45,7 @@ public void setId(Optional<Long> id) {
}

public Tag name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class User {
private Optional<Integer> userStatus = Optional.empty();

public User id(Long id) {
this.id = Optional.of(id);
this.id = Optional.ofNullable(id);
return this;
}

Expand All @@ -57,7 +57,7 @@ public void setId(Optional<Long> id) {
}

public User username(String username) {
this.username = Optional.of(username);
this.username = Optional.ofNullable(username);
return this;
}

Expand All @@ -76,7 +76,7 @@ public void setUsername(Optional<String> username) {
}

public User firstName(String firstName) {
this.firstName = Optional.of(firstName);
this.firstName = Optional.ofNullable(firstName);
return this;
}

Expand All @@ -95,7 +95,7 @@ public void setFirstName(Optional<String> firstName) {
}

public User lastName(String lastName) {
this.lastName = Optional.of(lastName);
this.lastName = Optional.ofNullable(lastName);
return this;
}

Expand All @@ -114,7 +114,7 @@ public void setLastName(Optional<String> lastName) {
}

public User email(String email) {
this.email = Optional.of(email);
this.email = Optional.ofNullable(email);
return this;
}

Expand All @@ -133,7 +133,7 @@ public void setEmail(Optional<String> email) {
}

public User password(String password) {
this.password = Optional.of(password);
this.password = Optional.ofNullable(password);
return this;
}

Expand All @@ -152,7 +152,7 @@ public void setPassword(Optional<String> password) {
}

public User phone(String phone) {
this.phone = Optional.of(phone);
this.phone = Optional.ofNullable(phone);
return this;
}

Expand All @@ -171,7 +171,7 @@ public void setPhone(Optional<String> phone) {
}

public User userStatus(Integer userStatus) {
this.userStatus = Optional.of(userStatus);
this.userStatus = Optional.ofNullable(userStatus);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AdditionalPropertiesAnyType {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesAnyType name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AdditionalPropertiesArray {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesArray name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AdditionalPropertiesBoolean {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesBoolean name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void setMapMapAnytype(Map<String, Map<String, Object>> mapMapAnytype) {
}

public AdditionalPropertiesClass anytype1(Object anytype1) {
this.anytype1 = Optional.of(anytype1);
this.anytype1 = Optional.ofNullable(anytype1);
return this;
}

Expand Down Expand Up @@ -325,7 +325,7 @@ public void setAnytype2(JsonNullable<Object> anytype2) {
}

public AdditionalPropertiesClass anytype3(Object anytype3) {
this.anytype3 = Optional.of(anytype3);
this.anytype3 = Optional.ofNullable(anytype3);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AdditionalPropertiesInteger {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesInteger name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AdditionalPropertiesNumber {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesNumber name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AdditionalPropertiesObject {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesObject name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AdditionalPropertiesString {
private Optional<String> name = Optional.empty();

public AdditionalPropertiesString name(String name) {
this.name = Optional.of(name);
this.name = Optional.ofNullable(name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void setClassName(String className) {
}

public Animal color(String color) {
this.color = Optional.of(color);
this.color = Optional.ofNullable(color);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public BigCat(String className) {
}

public BigCat kind(KindEnum kind) {
this.kind = Optional.of(kind);
this.kind = Optional.ofNullable(kind);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Capitalization {
private Optional<String> ATT_NAME = Optional.empty();

public Capitalization smallCamel(String smallCamel) {
this.smallCamel = Optional.of(smallCamel);
this.smallCamel = Optional.ofNullable(smallCamel);
return this;
}

Expand All @@ -56,7 +56,7 @@ public void setSmallCamel(Optional<String> smallCamel) {
}

public Capitalization capitalCamel(String capitalCamel) {
this.capitalCamel = Optional.of(capitalCamel);
this.capitalCamel = Optional.ofNullable(capitalCamel);
return this;
}

Expand All @@ -76,7 +76,7 @@ public void setCapitalCamel(Optional<String> capitalCamel) {
}

public Capitalization smallSnake(String smallSnake) {
this.smallSnake = Optional.of(smallSnake);
this.smallSnake = Optional.ofNullable(smallSnake);
return this;
}

Expand All @@ -96,7 +96,7 @@ public void setSmallSnake(Optional<String> smallSnake) {
}

public Capitalization capitalSnake(String capitalSnake) {
this.capitalSnake = Optional.of(capitalSnake);
this.capitalSnake = Optional.ofNullable(capitalSnake);
return this;
}

Expand All @@ -116,7 +116,7 @@ public void setCapitalSnake(Optional<String> capitalSnake) {
}

public Capitalization scAETHFlowPoints(String scAETHFlowPoints) {
this.scAETHFlowPoints = Optional.of(scAETHFlowPoints);
this.scAETHFlowPoints = Optional.ofNullable(scAETHFlowPoints);
return this;
}

Expand All @@ -136,7 +136,7 @@ public void setScAETHFlowPoints(Optional<String> scAETHFlowPoints) {
}

public Capitalization ATT_NAME(String ATT_NAME) {
this.ATT_NAME = Optional.of(ATT_NAME);
this.ATT_NAME = Optional.ofNullable(ATT_NAME);
return this;
}

Expand Down
Loading
Loading