diff --git a/src/main/java/org/springframework/data/mapping/Parameter.java b/src/main/java/org/springframework/data/mapping/Parameter.java index 39f1a2686f..bc2ad14143 100644 --- a/src/main/java/org/springframework/data/mapping/Parameter.java +++ b/src/main/java/org/springframework/data/mapping/Parameter.java @@ -33,6 +33,7 @@ * @param the type of the parameter * @author Oliver Gierke * @author Christoph Strobl + * @author Chris Bono */ public class Parameter> { @@ -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. * diff --git a/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java b/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java index dd5a3b745d..d189d7ad21 100755 --- a/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/ParameterUnitTests.java @@ -37,6 +37,7 @@ * * @author Oliver Gierke * @author Christoph Strobl + * @author Chris Bono */ @ExtendWith(MockitoExtension.class) class ParameterUnitTests

> { @@ -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) {