Skip to content

Add getRequiredName and hasName API to org.springframework.data.mapping.Parameter. #3272

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
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
26 changes: 26 additions & 0 deletions src/main/java/org/springframework/data/mapping/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @param <T> the type of the parameter
* @author Oliver Gierke
* @author Christoph Strobl
* @author Chris Bono
*/
public class Parameter<T, P extends PersistentProperty<P>> {

Expand Down Expand Up @@ -99,6 +100,31 @@ public String getName() {
return name;
}

/**
* Returns the required parameter name.
*
* @return the parameter name or throws {@link IllegalStateException} if the parameter does not have a name
* @since 3.5
*/
public String getRequiredName() {

if (!hasName()) {
throw new IllegalStateException("No name associated with this parameter");
}

return getName();
}

/**
* Returns whether the parameter has a name.
*
* @return whether the parameter has a name
* @since 3.5
*/
public boolean hasName() {
return this.name != null;
}

/**
* Returns the {@link TypeInformation} of the parameter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Chris Bono
*/
@ExtendWith(MockitoExtension.class)
class ParameterUnitTests<P extends PersistentProperty<P>> {
Expand Down Expand Up @@ -149,6 +150,35 @@ void shouldNotConsiderRecordMemberTypeOfClassEnclosingClassParameter() {
assertThat(iFace.isEnclosingClassParameter()).isFalse();
}

@Test // GH-3088
void getRequiredNameDoesNotThrowExceptionWhenHasName() {

var parameter = new Parameter<>("someName", type, annotations, entity);
assertThat(parameter.getRequiredName()).isEqualTo("someName");
}

@Test // GH-3088
void getRequiredNameThrowsExceptionWhenHasNoName() {

var parameter = new Parameter<>(null, type, annotations, entity);
assertThatIllegalStateException().isThrownBy(() -> parameter.getRequiredName())
.withMessage("No name associated with this parameter");
}

@Test // GH-3088
void hasNameReturnsTrueWhenHasName() {

var parameter = new Parameter<>("someName", type, annotations, entity);
assertThat(parameter.hasName()).isTrue();
}

@Test // GH-3088
void hasNameReturnsFalseWhenHasNoName() {

var parameter = new Parameter<>(null, type, annotations, entity);
assertThat(parameter.hasName()).isFalse();
}

interface IFace {

record RecordMember(IFace iFace) {
Expand Down