@@ -86,14 +86,14 @@ public function testGetResolvedSchemaTemplate()
8686 { "name": "items", "type": {"type": "array", "items": "com.example.Page" }, "default": [] }
8787 ]
8888 } ' ;
89- $ subschemaDefinition = '{
89+ $ subschemaDefinition = json_encode ( json_decode ( '{
9090 "type": "record",
9191 "namespace": "com.example",
9292 "name": "Page",
9393 "fields": [
9494 { "name": "number", "type": "int" }
9595 ]
96- } ' ;
96+ } ')) ;
9797
9898 $ expectedResult = str_replace ('"com.example.Page" ' , $ subschemaDefinition , $ rootDefinition );
9999
@@ -124,6 +124,104 @@ public function testGetResolvedSchemaTemplate()
124124 $ merger ->getResolvedSchemaTemplate ($ rootSchemaTemplate );
125125 }
126126
127+ public function testGetResolvedSchemaTemplateWithEmbeddedRoot (): void
128+ {
129+ $ rootDefinition = '{
130+ "type": "record",
131+ "namespace": "com.example",
132+ "schema_level": "root",
133+ "name": "Library",
134+ "fields": [
135+ {
136+ "name": "name",
137+ "type": "string"
138+ },
139+ {
140+ "name": "foundingYear",
141+ "type": [
142+ "null",
143+ "int"
144+ ],
145+ "default": null
146+ },
147+ {
148+ "name": "type",
149+ "type": [
150+ "null",
151+ {
152+ "name": "type",
153+ "type": "enum",
154+ "symbols": [
155+ "PUBLIC",
156+ "PRIVATE"
157+ ]
158+ }
159+ ],
160+ "default": null
161+ },
162+ {
163+ "name": "collection",
164+ "type": {
165+ "type": "array",
166+ "items": "com.example.Collection"
167+ },
168+ "default": []
169+ },
170+ {
171+ "name": "archive",
172+ "type": {
173+ "type": "array",
174+ "items": "com.example.Collection"
175+ },
176+ "default": []
177+ }
178+ ]
179+ } ' ;
180+ $ subschemaDefinition = json_encode (json_decode ('{
181+ "type": "record",
182+ "namespace": "com.example",
183+ "schema_level": "root",
184+ "name": "Collection",
185+ "fields": [
186+ { "name": "name", "type": "string" }
187+ ]
188+ } ' ));
189+
190+ $ subschemaDefinitionArray = \Safe \json_decode ($ subschemaDefinition , true );
191+ unset($ subschemaDefinitionArray ['schema_level ' ]);
192+ $ subschemaDefinitionWithoutLevel = json_encode ($ subschemaDefinitionArray );
193+
194+ $ subschemaId = '"com.example.Collection" ' ;
195+ $ pos = strpos ($ rootDefinition , $ subschemaId );
196+ $ expectedResult = substr_replace ($ rootDefinition , $ subschemaDefinitionWithoutLevel , $ pos , strlen ($ subschemaId ));
197+
198+ $ subschemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
199+ $ subschemaTemplate
200+ ->expects (self ::once ())
201+ ->method ('getSchemaDefinition ' )
202+ ->willReturn ($ subschemaDefinition );
203+ $ schemaRegistry = $ this ->getMockForAbstractClass (SchemaRegistryInterface::class);
204+ $ schemaRegistry
205+ ->expects (self ::once ())
206+ ->method ('getSchemaById ' )
207+ ->with ('com.example.Collection ' )
208+ ->willReturn ($ subschemaTemplate );
209+ $ rootSchemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
210+ $ rootSchemaTemplate
211+ ->expects (self ::once ())
212+ ->method ('getSchemaDefinition ' )
213+ ->willReturn ($ rootDefinition );
214+ $ rootSchemaTemplate
215+ ->expects (self ::once ())
216+ ->method ('withSchemaDefinition ' )
217+ ->with ($ expectedResult )
218+ ->willReturn ($ rootSchemaTemplate );
219+
220+ $ merger = new SchemaMerger ($ schemaRegistry );
221+
222+ $ merger ->getResolvedSchemaTemplate ($ rootSchemaTemplate );
223+ }
224+
127225 public function testGetResolvedSchemaTemplateWithMultiEmbedd ()
128226 {
129227 $ rootDefinition = $ this ->reformatJsonString ('{
@@ -304,14 +402,14 @@ public function testGetResolvedSchemaTemplateWithDifferentNamespaceForEmbeddedSc
304402 { "name": "items", "type": {"type": "array", "items": "com.example.other.Page" }, "default": [] }
305403 ]
306404 } ' ;
307- $ subschemaDefinition = '{
405+ $ subschemaDefinition = json_encode ( json_decode ( '{
308406 "type": "record",
309407 "namespace": "com.example.other",
310408 "name": "Page",
311409 "fields": [
312410 { "name": "number", "type": "int" }
313411 ]
314- } ' ;
412+ } ')) ;
315413
316414 $ expectedResult = str_replace ('"com.example.other.Page" ' , $ subschemaDefinition , $ rootDefinition );
317415
0 commit comments