@@ -51,6 +51,12 @@ class GoBuilder
5151 /** @var Type[] */
5252 public $ pathTypesDefined = [];
5353
54+ /** @var string[] */
55+ public $ pathByTypeName = [];
56+
57+ /** @var string[] */
58+ private $ typeNameByPath = [];
59+
5460 public function __construct ()
5561 {
5662 $ this ->code = new Code ();
@@ -160,22 +166,12 @@ private function makeStruct(Schema $schema, $path)
160166 $ this ->generatedStructsBySchema ->attach ($ schema , $ generatedStruct );
161167 $ generatedStruct ->schema = $ schema ;
162168
163- $ structName = '' ;
164- if ($ schema ->title ) {
165- $ structName = $ this ->codeBuilder ->exportableName ($ schema ->title );
166- }
167-
168- if (empty ($ structName )) {
169- if ($ path === '# ' ) {
170- $ structName = 'Untitled ' . ++$ this ->untitledIndex ;
171- } else {
172- $ structName = $ this ->codeBuilder ->exportableName ($ this ->pathToName ($ path ));
173- }
169+ $ pathToName = $ this ->pathToName ($ path );
170+ if ($ path === '# ' && empty ($ schema ->title )) {
171+ $ pathToName = 'Untitled ' . ++$ this ->untitledIndex ;
174172 }
175173
176- if (isset ($ this ->options ->renames [$ structName ])) {
177- $ structName = $ this ->options ->renames [$ structName ];
178- }
174+ $ structName = $ this ->typeName ($ schema , $ pathToName );
179175
180176 if (isset ($ this ->namesGenerated [$ structName ]) && $ schema ->getMeta (TypeBuilder::CONDITIONAL_META )) {
181177 $ structName = $ structName . 'Conditional ' ;
@@ -433,4 +429,46 @@ public function isNullable(Schema $schema)
433429
434430 return false ;
435431 }
432+
433+ /**
434+ * @param Schema $schema
435+ * @param string $path
436+ * @param StructDef|null $parentStruct
437+ */
438+ public function typeName ($ schema , $ path , $ parentStruct = null )
439+ {
440+ if (isset ($ this ->typeNameByPath [$ path ])) {
441+ return $ this ->typeNameByPath [$ path ];
442+ }
443+
444+ if (!empty ($ schema ->title )) {
445+ $ typeName = $ this ->codeBuilder ->exportableName ($ schema ->title , true );
446+ }
447+
448+ if (empty ($ typeName ) || (isset ($ this ->pathByTypeName [$ typeName ]) && $ this ->pathByTypeName [$ typeName ] !== $ path )) {
449+ $ typeName = $ this ->codeBuilder ->exportableName ($ path );
450+ }
451+
452+ if ($ parentStruct && false !== $ pos = strrpos ($ path , '-> ' )) {
453+ $ propertyName = substr ($ path , $ pos );
454+ $ typeName = $ this ->codeBuilder ->exportableName ($ parentStruct ->getName () . $ propertyName );
455+ }
456+
457+ if (isset ($ this ->options ->renames [$ typeName ])) {
458+ $ typeName = $ this ->options ->renames [$ typeName ];
459+ }
460+
461+ $ tn = $ typeName ;
462+ $ i = 2 ;
463+
464+ while (isset ($ this ->pathByTypeName [$ typeName ]) && $ this ->pathByTypeName [$ typeName ] !== $ path ) {
465+ $ typeName = $ tn . 'Type ' . $ i ;
466+ $ i ++;
467+ }
468+
469+ $ this ->pathByTypeName [$ typeName ] = $ path ;
470+ $ this ->typeNameByPath [$ path ] = $ typeName ;
471+
472+ return $ typeName ;
473+ }
436474}
0 commit comments