Skip to content

Commit

Permalink
Add test case for Java class with Optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kilink committed Jun 27, 2024
1 parent b1347d7 commit afc6b12
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 Netflix, 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
*
* http://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.netflix.graphql.dgs.internal.java.test.inputobjects;

import java.util.Optional;

public class JInputObjectWithOptional {
private Optional<String> foo = Optional.empty();
private Optional<JInputObjectWithSet> bar = Optional.empty();

public JInputObjectWithOptional() {}

public Optional<String> getFoo() {
return foo;
}

public void setFoo(Optional<String> foo) {
this.foo = foo;
}

public Optional<JInputObjectWithSet> getBar() {
return bar;
}

public void setBar(Optional<JInputObjectWithSet> bar) {
this.bar = bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import com.netflix.graphql.dgs.internal.java.test.inputobjects.JGenericSubInputO
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObject
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithKotlinProperty
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithMap
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithOptional
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithSet
import org.assertj.core.api.Assertions.COLLECTION
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -259,6 +261,17 @@ internal class InputObjectMapperTest {
assertThat(result.bar).isNotPresent
}

@Test
fun `mapping to a Java object with Optional fields works`() {
var result = inputObjectMapper.mapToJavaObject(mapOf<String, Any?>("foo" to null, "bar" to mapOf("items" to listOf(1, 2, 3, 4))), JInputObjectWithOptional::class.java)
assertThat(result.foo).isNotPresent
assertThat(result.bar).get().extracting("items").asInstanceOf(COLLECTION).containsExactly(1, 2, 3, 4)

result = inputObjectMapper.mapToJavaObject(mapOf<String, Any?>("foo" to "foo-value", "bar" to null), JInputObjectWithOptional::class.java)
assertThat(result.foo).get().isEqualTo("foo-value")
assertThat(result.bar).isNotPresent
}

data class KotlinInputObject(val simpleString: String?, val someDate: LocalDateTime, val someObject: KotlinSomeObject)
data class KotlinNestedInputObject(val input: KotlinInputObject)
data class KotlinDoubleNestedInputObject(val inputL1: KotlinNestedInputObject)
Expand Down

0 comments on commit afc6b12

Please sign in to comment.