-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[client] support optional input for jackson serializer (#1158)
* [client] support optional input for jackson serializer Wrap optional input arguments in new `OptionalInput` sealed class which can either be `Undefined` (omitted) or `Defined` (value or explicit null). Unfortunately it looks like we won't be able to support this functionality for `kotlinx-serialization` until Kotlin/kotlinx.serialization#1091 is resolved. Related: #1151 * fix build * add explicit opt-in flag for plugins to use optional input wrapper
- Loading branch information
1 parent
96676e3
commit 53539f5
Showing
44 changed files
with
636 additions
and
44 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
...ain/kotlin/com/expediagroup/graphql/client/jackson/serializers/OptionalInputSerializer.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,42 @@ | ||
/* | ||
* Copyright 2021 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.client.jackson.serializers | ||
|
||
import com.expediagroup.graphql.client.jackson.types.OptionalInput | ||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
|
||
class OptionalInputSerializer : JsonSerializer<OptionalInput<*>>() { | ||
|
||
override fun isEmpty(provider: SerializerProvider, value: OptionalInput<*>?): Boolean { | ||
return value == OptionalInput.Undefined | ||
} | ||
|
||
override fun serialize(value: OptionalInput<*>, gen: JsonGenerator, serializers: SerializerProvider) { | ||
when (value) { | ||
is OptionalInput.Undefined -> return | ||
is OptionalInput.Defined -> { | ||
if (value.value == null) { | ||
serializers.defaultNullValueSerializer.serialize(value.value, gen, serializers) | ||
} else { | ||
serializers.findValueSerializer(value.value::class.java).serialize(value.value, gen, serializers) | ||
} | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...nt-jackson/src/main/kotlin/com/expediagroup/graphql/client/jackson/types/OptionalInput.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,41 @@ | ||
/* | ||
* Copyright 2021 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.client.jackson.types | ||
|
||
import com.expediagroup.graphql.client.jackson.serializers.OptionalInputSerializer | ||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize | ||
|
||
@JsonSerialize(using = OptionalInputSerializer::class) | ||
sealed class OptionalInput<out T> { | ||
/** | ||
* Represents missing/undefined value. | ||
*/ | ||
@JsonSerialize(using = OptionalInputSerializer::class) | ||
object Undefined : OptionalInput<Nothing>() { | ||
override fun toString() = "Undefined" | ||
} | ||
|
||
/** | ||
* Wrapper holding explicitly specified value including NULL. | ||
*/ | ||
@JsonSerialize(using = OptionalInputSerializer::class) | ||
class Defined<out U> @JsonCreator constructor(@JsonValue val value: U?) : OptionalInput<U>() { | ||
override fun toString(): String = "Defined(value=$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
42 changes: 42 additions & 0 deletions
42
...client-jackson/src/test/kotlin/com/expediagroup/graphql/client/jackson/data/InputQuery.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,42 @@ | ||
/* | ||
* Copyright 2021 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.client.jackson.data | ||
|
||
import com.expediagroup.graphql.client.jackson.types.OptionalInput | ||
import com.expediagroup.graphql.client.types.GraphQLClientRequest | ||
import kotlin.reflect.KClass | ||
|
||
class InputQuery( | ||
override val variables: Variables | ||
) : GraphQLClientRequest<InputQuery.Result> { | ||
override val query: String = "INPUT_QUERY" | ||
|
||
override val operationName: String = "InputQuery" | ||
|
||
override fun responseType(): KClass<Result> = Result::class | ||
|
||
data class Variables( | ||
val requiredInput: Int, | ||
val optionalIntInput: OptionalInput<Int> = OptionalInput.Undefined, | ||
val optionalStringInput: OptionalInput<String> = OptionalInput.Undefined, | ||
val optionalBooleanInput: OptionalInput<Boolean> = OptionalInput.Undefined | ||
) | ||
|
||
data class Result( | ||
val stringResult: String | ||
) | ||
} |
73 changes: 73 additions & 0 deletions
73
...kotlin/com/expediagroup/graphql/client/jackson/serializers/OptionalInputSerializerTest.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,73 @@ | ||
/* | ||
* Copyright 2021 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.client.jackson.serializers | ||
|
||
import com.expediagroup.graphql.client.jackson.types.OptionalInput | ||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
class OptionalInputSerializerTest { | ||
|
||
private val mapper = jacksonObjectMapper() | ||
init { | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) | ||
} | ||
|
||
@Test | ||
fun `verify undefined value is serialized to empty JSON`() { | ||
val undefined = InputWrapper() | ||
assertEquals("{}", mapper.writeValueAsString(undefined)) | ||
} | ||
|
||
@Test | ||
fun `verify null value is serialized correctly`() { | ||
val explicitNull = InputWrapper( | ||
optionalInput = OptionalInput.Defined(null), | ||
optionalInputObject = OptionalInput.Defined(null), | ||
optionalInputList = OptionalInput.Defined(null) | ||
) | ||
assertEquals("""{"optionalInput":null,"optionalInputObject":null,"optionalInputList":null}""", mapper.writeValueAsString(explicitNull)) | ||
} | ||
|
||
@Test | ||
fun `verify defined values are serialized correctly`() { | ||
val defined = InputWrapper( | ||
optionalInput = OptionalInput.Defined(123), | ||
optionalInputObject = OptionalInput.Defined(BasicInput(OptionalInput.Defined("foo"))), | ||
optionalInputList = OptionalInput.Defined(listOf("a", "b", "c")) | ||
) | ||
assertEquals("""{"optionalInput":123,"optionalInputObject":{"name":"foo"},"optionalInputList":["a","b","c"]}""", mapper.writeValueAsString(defined)) | ||
} | ||
|
||
@Test | ||
fun `verify inner undefined values are serialized correctly`() { | ||
val nestedUndefined = InputWrapper(optionalInputObject = OptionalInput.Defined(BasicInput(name = OptionalInput.Undefined))) | ||
assertEquals("""{"optionalInputObject":{}}""", mapper.writeValueAsString(nestedUndefined)) | ||
} | ||
|
||
data class InputWrapper( | ||
val optionalInput: OptionalInput<Int> = OptionalInput.Undefined, | ||
val optionalInputObject: OptionalInput<BasicInput> = OptionalInput.Undefined, | ||
val optionalInputList: OptionalInput<List<String>> = OptionalInput.Undefined | ||
) | ||
|
||
data class BasicInput( | ||
val name: OptionalInput<String> = OptionalInput.Undefined | ||
) | ||
} |
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
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
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
Oops, something went wrong.