@@ -28,9 +28,13 @@ class RequestGenerator extends MutatorAccessorClassGeneratorAbstract
2828
2929 public const SUBDIRECTORY = 'Request/ ' ;
3030
31+ private const CONTENT_TYPE_PARAMETER_NAME = 'contentType ' ;
32+
3133 /** @var SecurityStrategyInterface[] */
3234 private array $ securityStrategies ;
3335
36+ private bool $ isContentTypeEnum = false ;
37+
3438 public function __construct (
3539 string $ baseNamespace ,
3640 CodeBuilder $ builder ,
@@ -45,6 +49,7 @@ public function __construct(
4549 public function generate (Specification $ specification , PhpFileCollection $ fileRegistry ): void
4650 {
4751 foreach ($ specification ->getOperations () as $ operation ) {
52+ $ this ->isContentTypeEnum = false ;
4853 $ this ->generateRequest ($ fileRegistry , $ operation , $ specification );
4954 }
5055 }
@@ -92,6 +97,7 @@ protected function generateEnums(Request $request): array
9297 protected function generateProperties (Request $ request , Operation $ operation , Specification $ specification ): array
9398 {
9499 $ statements = [];
100+ $ fieldNames = [];
95101
96102 foreach ($ request ->fields as $ field ) {
97103 if ($ field ->isComposite ()) {
@@ -117,13 +123,21 @@ protected function generateProperties(Request $request, Operation $operation, Sp
117123 }
118124
119125 $ statements [] = $ this ->generateProperty ($ field );
126+ $ fieldNames [] = $ field ->getPhpVariableName ();
120127 }
121128
122- $ default = null ;
123- if (count ($ request ->bodyContentTypes ) < 2 ) {
129+ $ default = null ;
130+ $ bodyContentTypesCount = count ($ request ->bodyContentTypes );
131+ if ($ bodyContentTypesCount < 2 ) {
124132 $ default = $ this ->builder ->val ($ request ->bodyContentTypes [0 ] ?? '' );
125133 }
126- $ statements [] = $ this ->builder ->localProperty ('contentType ' , 'string ' , 'string ' , false , $ default );
134+
135+ if (
136+ ($ bodyContentTypesCount < 2 || !$ this ->phpVersion ->isConstructorPropertyPromotionSupported ())
137+ && !in_array (self ::CONTENT_TYPE_PARAMETER_NAME , $ fieldNames , true )
138+ ) {
139+ $ statements [] = $ this ->builder ->localProperty (self ::CONTENT_TYPE_PARAMETER_NAME , 'string ' , 'string ' , false , $ default );
140+ }
127141
128142 foreach ($ this ->securityStrategies as $ securityStrategy ) {
129143 array_push ($ statements , ...$ securityStrategy ->getProperties ($ operation , $ specification ));
@@ -137,6 +151,7 @@ protected function generateConstructor(
137151 Operation $ operation ,
138152 Specification $ specification
139153 ): ?ClassMethod {
154+ $ paramNames = [];
140155 $ params = [];
141156 $ paramInits = [];
142157 $ validations = [];
@@ -163,7 +178,16 @@ protected function generateConstructor(
163178 }
164179 }
165180
166- $ params [] = $ param ;
181+ $ params [] = $ param ;
182+ $ paramNames [] = $ field ->getPhpVariableName ();
183+
184+ if (
185+ $ field ->isEnum ()
186+ && $ this ->phpVersion ->isEnumSupported ()
187+ && $ field ->getPhpVariableName () === self ::CONTENT_TYPE_PARAMETER_NAME
188+ ) {
189+ $ this ->isContentTypeEnum = true ;
190+ }
167191
168192 $ paramInits [] = $ this ->builder ->assign (
169193 $ this ->builder ->localPropertyFetch ($ field ->getPhpVariableName ()),
@@ -177,14 +201,12 @@ protected function generateConstructor(
177201 array_push ($ paramInits , ...$ securityStrategy ->getConstructorParamInits ($ operation , $ specification ));
178202 }
179203
180- if (count ($ request ->bodyContentTypes ) > 1 ) {
181- $ contentTypeVariableName = 'contentType ' ;
182-
183- $ params [] = $ this ->builder ->param ($ contentTypeVariableName )->setType ('string ' );
204+ if (count ($ request ->bodyContentTypes ) > 1 && !in_array (self ::CONTENT_TYPE_PARAMETER_NAME , $ paramNames , true )) {
205+ $ params [] = $ this ->builder ->param (self ::CONTENT_TYPE_PARAMETER_NAME )->setType ('string ' );
184206
185207 $ paramInits [] = $ this ->builder ->assign (
186- $ this ->builder ->localPropertyFetch ($ contentTypeVariableName ),
187- $ this ->builder ->var ($ contentTypeVariableName )
208+ $ this ->builder ->localPropertyFetch (self :: CONTENT_TYPE_PARAMETER_NAME ),
209+ $ this ->builder ->var (self :: CONTENT_TYPE_PARAMETER_NAME )
188210 );
189211 }
190212
@@ -242,8 +264,14 @@ private function generateSetters(Request $request): array
242264
243265 private function generateGetContentType (): ClassMethod
244266 {
245- $ return = $ this ->builder ->return ($ this ->builder ->localPropertyFetch ('contentType ' ));
246- $ returnType = 'string ' ;
267+ $ localProperty = $ this ->builder ->localPropertyFetch (self ::CONTENT_TYPE_PARAMETER_NAME );
268+ $ returnType = 'string ' ;
269+
270+ if ($ this ->isContentTypeEnum ) {
271+ $ localProperty = $ this ->builder ->propertyFetch ($ localProperty , 'value ' );
272+ }
273+
274+ $ return = $ this ->builder ->return ($ localProperty );
247275
248276 return $ this
249277 ->builder
@@ -452,8 +480,15 @@ private function generateGetHeadersMethod(
452480 $ stmts = $ this ->getSecurityHeadersStmts ($ operation , $ specification );
453481 $ headers = $ this ->getSecurityHeaders ($ operation , $ specification );
454482 if (!empty ($ request ->bodyContentTypes )) {
455- $ headers ['Content-Type ' ] = $ this ->builder ->localPropertyFetch ('contentType ' );
483+ $ contentType = $ this ->builder ->localPropertyFetch ('contentType ' );
484+
485+ if ($ this ->isContentTypeEnum ) {
486+ $ contentType = $ this ->builder ->propertyFetch ($ contentType , 'value ' );
487+ }
488+
489+ $ headers ['Content-Type ' ] = $ contentType ;
456490 }
491+
457492 $ returnVal = $ this ->builder ->array ($ headers );
458493 $ fieldsArr = [];
459494 $ returnType = 'array ' ;
0 commit comments