@@ -175,24 +175,31 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
175175
176176 var parameters = new List < ValueExpression > ( ) ;
177177 var additionalPropertyIndex = GetAdditionalPropertyIndex ( ) ;
178- for ( int i = 0 , fullConstructorParameterIndex = 0 ; i < flattenedProperties . Count ; i ++ , fullConstructorParameterIndex ++ )
178+ for ( int flattenedPropertyIndex = 0 , fullConstructorParameterIndex = 0 ; ; fullConstructorParameterIndex ++ )
179179 {
180- if ( i == additionalPropertyIndex )
180+ // If we have processed all the flattened properties or all the constructor parameters, we can break the loop.
181+ if ( flattenedPropertyIndex >= flattenedProperties . Count || fullConstructorParameterIndex >= fullConstructorParameters . Count )
182+ {
183+ break ;
184+ }
185+
186+ if ( fullConstructorParameterIndex == additionalPropertyIndex )
181187 {
182188 // If the additionalProperties parameter exists, we need to pass a new instance for it.
183189 parameters . Add ( New . Instance ( new CSharpType ( typeof ( Dictionary < string , BinaryData > ) ) ) ) ;
184190
185191 // If the additionalProperties parameter is the last parameter, we can break the loop.
186- if ( additionalPropertyIndex == fullConstructorParameters . Count - 1 )
192+ if ( fullConstructorParameterIndex == fullConstructorParameters . Count - 1 )
187193 {
188194 break ;
189195 }
190196 fullConstructorParameterIndex ++ ;
191197 }
192- var ( isOverriddenValueType , flattenedProperty ) = flattenedProperties [ i ] ;
198+ var ( isOverriddenValueType , flattenedProperty ) = flattenedProperties [ flattenedPropertyIndex ] ;
193199 var propertyParameter = flattenedProperty . AsParameter ;
194200 var flattenedPropertyType = flattenedProperty . Type ;
195201 var constructorParameterType = fullConstructorParameters [ fullConstructorParameterIndex ] . Type ;
202+
196203 // If the internal property type is the same as the property type, we can use the flattened property directly.
197204 if ( constructorParameterType . AreNamesEqual ( flattenedPropertyType ) )
198205 {
@@ -204,6 +211,8 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
204211 {
205212 parameters . Add ( isOverriddenValueType ? propertyParameter . Property ( "Value" ) : propertyParameter ) ;
206213 }
214+ // only increase flattenedPropertyIndex when we use a flattened property
215+ flattenedPropertyIndex ++ ;
207216 }
208217 else
209218 {
@@ -219,8 +228,8 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
219228 }
220229 }
221230
222- // If the additionalProperties parameter exists at the end, we need to pass a new instance for it.
223- if ( additionalPropertyIndex == propertyModelType ! . FullConstructor . Signature . Parameters . Count - 1 )
231+ // If the additionalProperties parameter is missing at the end, we need to pass a new instance for it.
232+ if ( parameters . Count < fullConstructorParameters . Count && additionalPropertyIndex == propertyModelType ! . FullConstructor . Signature . Parameters . Count - 1 )
224233 {
225234 parameters . Add ( New . Instance ( new CSharpType ( typeof ( Dictionary < string , BinaryData > ) ) ) ) ;
226235 }
0 commit comments