1212using  Microsoft . OData . Edm . Vocabularies ; 
1313using  Microsoft . OpenApi . OData . Edm ; 
1414using  Microsoft . OpenApi . OData . Vocabulary . Capabilities ; 
15+ using  System . Diagnostics ; 
16+ using  System ; 
1517
1618namespace  Microsoft . OpenApi . OData . Generator 
1719{ 
@@ -151,19 +153,20 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
151153        /// <param name="context">The OData context.</param> 
152154        /// <param name="keySegment">The key segment.</param> 
153155        /// <param name="parameterNameMapping">The parameter name mapping.</param> 
154-         /// <returns>The created the  list of <see cref="OpenApiParameter"/>.</returns> 
156+         /// <returns>The created list of <see cref="OpenApiParameter"/>.</returns> 
155157        public  static IList < OpenApiParameter >  CreateKeyParameters ( this  ODataContext  context ,  ODataKeySegment  keySegment , 
156158            IDictionary < string ,  string >  parameterNameMapping  =  null ) 
157159        { 
158160            Utils . CheckArgumentNull ( context ,  nameof ( context ) ) ; 
159161            Utils . CheckArgumentNull ( keySegment ,  nameof ( keySegment ) ) ; 
162+             
163+             if  ( keySegment . IsAlternateKey ) 
164+                 return  CreateAlternateKeyParameters ( context ,  keySegment ) ; 
160165
161166            IEdmEntityType  entityType  =  keySegment . EntityType ; 
162-             if  ( keySegment . IsAlternateKey ) 
163-                 return  CreateAlternateKeyParameters ( context ,  entityType ) ; 
167+             IList < IEdmStructuralProperty >  keys  =  entityType . Key ( ) . ToList ( ) ; 
164168
165169            List < OpenApiParameter >  parameters  =  new ( ) ; 
166-             IList < IEdmStructuralProperty >  keys  =  entityType . Key ( ) . ToList ( ) ; 
167170            if  ( keys . Count ( )  ==  1 ) 
168171            { 
169172                string  keyName  =  keys . First ( ) . Name ; 
@@ -177,7 +180,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
177180
178181                OpenApiParameter  parameter  =  new  OpenApiParameter 
179182                { 
180-                     Name  =  parameterNameMapping  ==  null  ?  keyName :  parameterNameMapping [ keyName ] , 
183+                     Name  =  parameterNameMapping  ==  null  ?  keyName   :  parameterNameMapping [ keyName ] , 
181184                    In  =  ParameterLocation . Path , 
182185                    Required  =  true , 
183186                    Description  =  $ "The unique identifier of { entityType . Name } ", 
@@ -195,7 +198,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
195198                    OpenApiParameter  parameter  =  new  OpenApiParameter 
196199                    { 
197200                        Name  =  parameterNameMapping  ==  null  ? 
198-                             keyProperty . Name : 
201+                             keyProperty . Name   : 
199202                            parameterNameMapping [ keyProperty . Name ] , // By design: not prefix with type name if enable type name prefix 
200203                        In  =  ParameterLocation . Path , 
201204                        Required  =  true , 
@@ -216,15 +219,28 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
216219            return  parameters ; 
217220        } 
218221
219-         private  static IList < OpenApiParameter >  CreateAlternateKeyParameters ( ODataContext  context ,  IEdmEntityType  entityType ) 
222+ 
223+         /// <summary> 
224+         /// Create alternate key parameters for the <see cref="ODataKeySegment"/>. 
225+         /// </summary> 
226+         /// <param name="context">The OData context.</param> 
227+         /// <param name="keySegment">The key segment.</param> 
228+         /// <returns>A list of <see cref="OpenApiParameter"/> of alternate key parameters.</returns> 
229+         private  static IList < OpenApiParameter >  CreateAlternateKeyParameters ( ODataContext  context ,  ODataSegment  keySegment ) 
220230        { 
231+             Debug . Assert ( keySegment . Kind  ==  ODataSegmentKind . Key ) ; 
232+             
221233            IList < OpenApiParameter >  parameters  =  new  List < OpenApiParameter > ( ) ; 
222-             IEnumerable < IDictionary < string ,  IEdmProperty > >  alternateKeys  =  context . Model . GetAlternateKeysAnnotation ( entityType ) ; 
234+             IEdmEntityType  entityType  =  keySegment . EntityType ; 
235+             IEnumerable < IDictionary < string ,  IEdmProperty > >  alternateKeys  =  context . Model . GetAlternateKeysAnnotation ( entityType ) ;             
236+             
223237            foreach  ( var  alternateKey  in  alternateKeys ) 
224238            { 
225239                if  ( alternateKey . Count ( )  ==  1 ) 
226240                { 
227-                     parameters . Add ( 
241+                     if  ( keySegment . Identifier . Equals ( alternateKey . First ( ) . Key ,  StringComparison . OrdinalIgnoreCase ) ) 
242+                     { 
243+                         parameters . Add ( 
228244                        new  OpenApiParameter 
229245                        { 
230246                            Name  =  alternateKey . First ( ) . Key , 
@@ -234,12 +250,15 @@ private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext
234250                            Required  =  true 
235251                        } 
236252                     ) ; 
253+                     }                     
237254                } 
238255                else 
239256                { 
240257                    foreach  ( var  compositekey  in  alternateKey ) 
241258                    { 
242-                         parameters . Add ( 
259+                         if  ( keySegment . Identifier . Contains ( compositekey . Key ) ) 
260+                         { 
261+                             parameters . Add ( 
243262                            new  OpenApiParameter 
244263                            { 
245264                                Name  =  compositekey . Key , 
@@ -248,7 +267,8 @@ private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext
248267                                Schema  =  context . CreateEdmTypeSchema ( compositekey . Value . Type ) , 
249268                                Required  =  true 
250269                            } 
251-                         ) ; 
270+                          ) ; 
271+                         }                         
252272                    } 
253273                } 
254274            } 
0 commit comments