-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add OptionalInput type to allow explicit null value detection
- Loading branch information
Showing
5 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
spring-graphql/src/main/java/org/springframework/graphql/data/method/OptionalInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.springframework.graphql.data.method; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Wrapper used to represent optionally defined input arguments that allows us to distinguish between undefined value, explicit NULL value | ||
* and specified value. | ||
*/ | ||
public class OptionalInput<T> { | ||
|
||
public static<T> OptionalInput<T> defined(@Nullable T value) { | ||
return new OptionalInput.Defined<>(value); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static<T> OptionalInput<T> undefined() { | ||
return (OptionalInput<T>)new OptionalInput.Undefined(); | ||
} | ||
|
||
/** | ||
* Represents missing/undefined value. | ||
*/ | ||
public static class Undefined extends OptionalInput<Void> { | ||
@Override | ||
public boolean equals(Object obj) { | ||
return obj.getClass() == this.getClass(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return super.hashCode(); | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper holding explicitly specified value including NULL. | ||
*/ | ||
public static class Defined<T> extends OptionalInput<T> { | ||
@Nullable | ||
private final T value; | ||
|
||
public Defined(@Nullable T value) { | ||
this.value = value; | ||
} | ||
|
||
@Nullable | ||
public T getValue() { | ||
return value; | ||
} | ||
|
||
public Boolean isEmpty() { | ||
return value == null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Defined<?> defined = (Defined<?>) o; | ||
if (isEmpty() == defined.isEmpty()) return true; | ||
if (value == null) return false; | ||
return value.equals(defined.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...mework/graphql/data/method/annotation/support/OptionalInputArgumentConversionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.springframework.graphql.data.method.annotation.support; | ||
|
||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
import org.springframework.graphql.data.method.OptionalInput; | ||
|
||
public class OptionalInputArgumentConversionService implements ConversionService { | ||
@Override | ||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { | ||
return targetType.getType() == OptionalInput.class; | ||
} | ||
|
||
@Override | ||
public <T> T convert(Object source, Class<T> targetType) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { | ||
return OptionalInput.defined(source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...c/test/java/org/springframework/graphql/data/method/annotation/support/KotlinBookInput.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.springframework.graphql.data.method.annotation.support; | ||
|
||
import org.springframework.graphql.data.method.OptionalInput | ||
|
||
data class KotlinBookInput( | ||
val name: String, val authorId: Long, | ||
val notes: OptionalInput<String?> | ||
) |