1717use Shopware \Core \Framework \DataAbstractionLayer \Field \ManyToManyAssociationField ;
1818use Shopware \Core \Framework \DataAbstractionLayer \Field \OneToManyAssociationField ;
1919use Swh \SmartRelationSync \DataAbstractionLayer \WriteCommandExtractorDecorator ;
20+ use Symfony \Component \Serializer \NameConverter \CamelCaseToSnakeCaseNameConverter ;
2021
2122class OpenApiDefinitionSchemaBuilderDecorator extends OpenApiDefinitionSchemaBuilder
2223{
24+ private readonly CamelCaseToSnakeCaseNameConverter $ converter ;
25+
26+ public function __construct ()
27+ {
28+ parent ::__construct ();
29+
30+ $ this ->converter = new CamelCaseToSnakeCaseNameConverter (null , false );
31+ }
32+
2333 /**
2434 * @return Schema[]
2535 */
@@ -32,15 +42,15 @@ public function getSchemaByDefinition(
3242 ): array {
3343 $ schemas = parent ::getSchemaByDefinition ($ definition , $ path , $ forSalesChannel , $ onlyFlat , $ apiType );
3444
35- if (count ($ schemas ) !== 1 ) {
45+ $ relevantSchemas = $ this ->getRelevantSchemas ($ schemas , $ definition );
46+
47+ if (count ($ relevantSchemas ) === 0 ) {
3648 return $ schemas ;
3749 }
3850
3951 $ relevantFields = $ definition ->getFields ()
4052 ->filter (fn (Field $ field ) => $ this ->isRelevantField ($ field ));
4153
42- $ schema = current ($ schemas );
43-
4454 foreach ($ relevantFields as $ field ) {
4555 if (!$ this ->shouldFieldBeIncluded ($ field , $ forSalesChannel )) {
4656 continue ;
@@ -53,12 +63,46 @@ public function getSchemaByDefinition(
5363 'type ' => 'boolean ' ,
5464 ]);
5565
56- $ schema ->properties [] = $ property ;
66+ foreach ($ relevantSchemas as $ schema ) {
67+ $ schema ->properties [] = $ property ;
68+ }
5769 }
5870
5971 return $ schemas ;
6072 }
6173
74+ /**
75+ * @param Schema[] $schemas
76+ *
77+ * @return Schema[]
78+ */
79+ private function getRelevantSchemas (array $ schemas , EntityDefinition $ definition ): array
80+ {
81+ $ schemaName = $ this ->snakeCaseToCamelCase ($ definition ->getEntityName ());
82+
83+ if (!array_key_exists ($ schemaName , $ schemas )) {
84+ return [];
85+ }
86+
87+ $ relevantSchemas = [$ schemas [$ schemaName ]];
88+
89+ $ schemaNameJsonApi = $ schemaName . 'JsonApi ' ;
90+
91+ if (!array_key_exists ($ schemaNameJsonApi , $ schemas )) {
92+ return $ relevantSchemas ;
93+ }
94+
95+ $ jsonApiSchema = $ schemas [$ schemaNameJsonApi ];
96+
97+ $ childSchema = $ jsonApiSchema ->allOf [1 ] ?? null ;
98+
99+ if ($ childSchema instanceof Schema) {
100+ $ relevantSchemas [] = $ childSchema ;
101+ }
102+
103+ return $ relevantSchemas ;
104+ }
105+
62106 /**
63107 * @phpstan-assert-if-true ManyToManyAssociationField|OneToManyAssociationField $field
64108 */
@@ -91,4 +135,9 @@ private function shouldFieldBeIncluded(Field $field, bool $forSalesChannel): boo
91135
92136 return true ;
93137 }
138+
139+ private function snakeCaseToCamelCase (string $ input ): string
140+ {
141+ return $ this ->converter ->denormalize ($ input );
142+ }
94143}
0 commit comments