@@ -33,4 +33,158 @@ class OneOfInputObjectRulesTest extends Specification {
3333 schemaProblem. errors[1 ]. description == " OneOf input field OneOfInputType.badDefaulted cannot have a default value."
3434 schemaProblem. errors[1 ]. classification == SchemaValidationErrorType.OneOfDefaultValueOnField
3535 }
36+
37+ def " oneOf with scalar fields is inhabited" () {
38+ def sdl = """
39+ type Query { f(arg: A): String }
40+ input A @oneOf { a: String, b: Int }
41+ """
42+
43+ when :
44+ def registry = new SchemaParser (). parse(sdl)
45+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
46+
47+ then :
48+ noExceptionThrown()
49+ }
50+
51+ def " oneOf with enum field is inhabited" () {
52+ def sdl = """
53+ type Query { f(arg: A): String }
54+ enum Color { RED GREEN BLUE }
55+ input A @oneOf { a: Color }
56+ """
57+
58+ when :
59+ def registry = new SchemaParser (). parse(sdl)
60+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
61+
62+ then :
63+ noExceptionThrown()
64+ }
65+
66+ def " oneOf with list field is inhabited" () {
67+ def sdl = """
68+ type Query { f(arg: A): String }
69+ input A @oneOf { a: [A] }
70+ """
71+
72+ when :
73+ def registry = new SchemaParser (). parse(sdl)
74+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
75+
76+ then :
77+ noExceptionThrown()
78+ }
79+
80+ def " oneOf referencing non-oneOf input is inhabited" () {
81+ def sdl = """
82+ type Query { f(arg: A): String }
83+ input A @oneOf { a: RegularInput }
84+ input RegularInput { x: String }
85+ """
86+
87+ when :
88+ def registry = new SchemaParser (). parse(sdl)
89+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
90+
91+ then :
92+ noExceptionThrown()
93+ }
94+
95+ def " oneOf with escape field is inhabited" () {
96+ def sdl = """
97+ type Query { f(arg: A): String }
98+ input A @oneOf { b: B, escape: String }
99+ input B @oneOf { a: A }
100+ """
101+
102+ when :
103+ def registry = new SchemaParser (). parse(sdl)
104+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
105+
106+ then :
107+ noExceptionThrown()
108+ }
109+
110+ def " mutually referencing oneOf types with scalar escape is inhabited" () {
111+ def sdl = """
112+ type Query { f(arg: A): String }
113+ input A @oneOf { b: B }
114+ input B @oneOf { a: A, escape: Int }
115+ """
116+
117+ when :
118+ def registry = new SchemaParser (). parse(sdl)
119+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
120+
121+ then :
122+ noExceptionThrown()
123+ }
124+
125+ def " oneOf referencing non-oneOf with back-reference is inhabited" () {
126+ def sdl = """
127+ type Query { f(arg: A): String }
128+ input A @oneOf { b: RegularInput }
129+ input RegularInput { back: A }
130+ """
131+
132+ when :
133+ def registry = new SchemaParser (). parse(sdl)
134+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
135+
136+ then :
137+ noExceptionThrown()
138+ }
139+
140+ def " multiple fields with chained oneOf escape is inhabited" () {
141+ def sdl = """
142+ type Query { f(arg: A): String }
143+ input A @oneOf { b: B, c: C }
144+ input B @oneOf { a: A }
145+ input C @oneOf { a: A, escape: String }
146+ """
147+
148+ when :
149+ def registry = new SchemaParser (). parse(sdl)
150+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
151+
152+ then :
153+ noExceptionThrown()
154+ }
155+
156+ def " single oneOf self-reference cycle is not inhabited" () {
157+ def sdl = """
158+ type Query { f(arg: A): String }
159+ input A @oneOf { self: A }
160+ """
161+
162+ when :
163+ def registry = new SchemaParser (). parse(sdl)
164+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
165+
166+ then :
167+ def schemaProblem = thrown(InvalidSchemaException )
168+ schemaProblem. errors. size() == 1
169+ schemaProblem. errors[0 ]. description == " OneOf Input Object A must be inhabited but all fields recursively reference only other OneOf Input Objects forming an unresolvable cycle."
170+ schemaProblem. errors[0 ]. classification == SchemaValidationErrorType.OneOfNotInhabited
171+ }
172+
173+ def " multiple oneOf types forming cycle are not inhabited" () {
174+ def sdl = """
175+ type Query { f(arg: A): String }
176+ input A @oneOf { b: B }
177+ input B @oneOf { c: C }
178+ input C @oneOf { a: A }
179+ """
180+
181+ when :
182+ def registry = new SchemaParser (). parse(sdl)
183+ new SchemaGenerator (). makeExecutableSchema(registry, TestUtil . getMockRuntimeWiring())
184+
185+ then :
186+ def schemaProblem = thrown(InvalidSchemaException )
187+ schemaProblem. errors. size() == 3
188+ schemaProblem. errors. every { it. classification == SchemaValidationErrorType.OneOfNotInhabited }
189+ }
36190}
0 commit comments