@@ -95,14 +95,14 @@ public function testGetResolvedSchemaTemplate(): void
9595 { "name": "items", "type": {"type": "array", "items": "com.example.Page" }, "default": [] }
9696 ]
9797 } ' ;
98- $ subschemaDefinition = '{
98+ $ subschemaDefinition = json_encode ( json_decode ( '{
9999 "type": "record",
100100 "namespace": "com.example",
101101 "name": "Page",
102102 "fields": [
103103 { "name": "number", "type": "int" }
104104 ]
105- } ' ;
105+ } ')) ;
106106
107107 $ expectedResult = str_replace ('"com.example.Page" ' , $ subschemaDefinition , $ rootDefinition );
108108
@@ -134,6 +134,105 @@ public function testGetResolvedSchemaTemplate(): void
134134 $ merger ->getResolvedSchemaTemplate ($ rootSchemaTemplate );
135135 }
136136
137+ public function testGetResolvedSchemaTemplateWithEmbeddedRoot (): void
138+ {
139+ $ rootDefinition = '{
140+ "type": "record",
141+ "namespace": "com.example",
142+ "schema_level": "root",
143+ "name": "Library",
144+ "fields": [
145+ {
146+ "name": "name",
147+ "type": "string"
148+ },
149+ {
150+ "name": "foundingYear",
151+ "type": [
152+ "null",
153+ "int"
154+ ],
155+ "default": null
156+ },
157+ {
158+ "name": "type",
159+ "type": [
160+ "null",
161+ {
162+ "name": "type",
163+ "type": "enum",
164+ "symbols": [
165+ "PUBLIC",
166+ "PRIVATE"
167+ ]
168+ }
169+ ],
170+ "default": null
171+ },
172+ {
173+ "name": "collection",
174+ "type": {
175+ "type": "array",
176+ "items": "com.example.Collection"
177+ },
178+ "default": []
179+ },
180+ {
181+ "name": "archive",
182+ "type": {
183+ "type": "array",
184+ "items": "com.example.Collection"
185+ },
186+ "default": []
187+ }
188+ ]
189+ } ' ;
190+ $ subschemaDefinition = json_encode (json_decode ('{
191+ "type": "record",
192+ "namespace": "com.example",
193+ "schema_level": "root",
194+ "name": "Collection",
195+ "fields": [
196+ { "name": "name", "type": "string" }
197+ ]
198+ } ' ));
199+
200+ $ subschemaDefinitionArray = \Safe \json_decode ($ subschemaDefinition , true );
201+ unset($ subschemaDefinitionArray ['schema_level ' ]);
202+ $ subschemaDefinitionWithoutLevel = json_encode ($ subschemaDefinitionArray );
203+
204+ $ subschemaId = '"com.example.Collection" ' ;
205+ $ pos = strpos ($ rootDefinition , $ subschemaId );
206+ $ expectedResult = substr_replace ($ rootDefinition , $ subschemaDefinitionWithoutLevel , $ pos , strlen ($ subschemaId ));
207+
208+ $ subschemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
209+ $ subschemaTemplate
210+ ->expects (self ::once ())
211+ ->method ('getSchemaDefinition ' )
212+ ->willReturn ($ subschemaDefinition );
213+ $ schemaRegistry = $ this ->getMockForAbstractClass (SchemaRegistryInterface::class);
214+ $ schemaRegistry
215+ ->expects (self ::once ())
216+ ->method ('getSchemaById ' )
217+ ->with ('com.example.Collection ' )
218+ ->willReturn ($ subschemaTemplate );
219+ $ rootSchemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
220+ $ rootSchemaTemplate
221+ ->expects (self ::once ())
222+ ->method ('getSchemaDefinition ' )
223+ ->willReturn ($ rootDefinition );
224+ $ rootSchemaTemplate
225+ ->expects (self ::once ())
226+ ->method ('withSchemaDefinition ' )
227+ ->with ($ expectedResult )
228+ ->willReturn ($ rootSchemaTemplate );
229+
230+ $ merger = new SchemaMerger ();
231+ $ merger ->setSchemaRegistry ($ schemaRegistry );
232+
233+ $ merger ->getResolvedSchemaTemplate ($ rootSchemaTemplate );
234+ }
235+
137236 public function testGetResolvedSchemaTemplateWithMultiEmbedd (): void
138237 {
139238 $ rootDefinition = $ this ->reformatJsonString ('{
@@ -315,14 +414,14 @@ public function testGetResolvedSchemaTemplateWithDifferentNamespaceForEmbeddedSc
315414 { "name": "items", "type": {"type": "array", "items": "com.example.other.Page" }, "default": [] }
316415 ]
317416 } ' ;
318- $ subschemaDefinition = '{
417+ $ subschemaDefinition = json_encode ( json_decode ( '{
319418 "type": "record",
320419 "namespace": "com.example.other",
321420 "name": "Page",
322421 "fields": [
323422 { "name": "number", "type": "int" }
324423 ]
325- } ' ;
424+ } ')) ;
326425
327426 $ expectedResult = str_replace ('"com.example.other.Page" ' , $ subschemaDefinition , $ rootDefinition );
328427
0 commit comments