@@ -107,6 +107,14 @@ public static IDictionary<string, OpenApiResponse> CreateResponses(this ODataCon
107107 if ( context . HasAnyNonContainedCollections ( ) )
108108 responses [ $ "String{ Constants . CollectionSchemaSuffix } "] = CreateCollectionResponse ( "String" ) ;
109109
110+ foreach ( IEdmOperation operation in context . Model . SchemaElements . OfType < IEdmOperation > ( )
111+ . Where ( op => context . Model . OperationTargetsMultiplePaths ( op ) ) )
112+ {
113+ OpenApiResponse response = context . CreateOperationResponse ( operation ) ;
114+ if ( response != null )
115+ responses [ $ "{ operation . Name } Response"] = response ;
116+ }
117+
110118 return responses ;
111119 }
112120
@@ -115,110 +123,130 @@ public static IDictionary<string, OpenApiResponse> CreateResponses(this ODataCon
115123 /// </summary>
116124 /// <param name="context">The OData context.</param>
117125 /// <param name="operationImport">The Edm operation import.</param>
118- /// <param name="path">The OData path.</param>
119126 /// <returns>The created <see cref="OpenApiResponses"/>.</returns>
120- public static OpenApiResponses CreateResponses ( this ODataContext context , IEdmOperationImport operationImport , ODataPath path )
127+ public static OpenApiResponses CreateResponses ( this ODataContext context , IEdmOperationImport operationImport )
121128 {
122129 Utils . CheckArgumentNull ( context , nameof ( context ) ) ;
123130 Utils . CheckArgumentNull ( operationImport , nameof ( operationImport ) ) ;
124- Utils . CheckArgumentNull ( path , nameof ( path ) ) ;
125131
126- return context . CreateResponses ( operationImport . Operation , path ) ;
132+ return context . CreateResponses ( operationImport . Operation ) ;
127133 }
128134
129135 /// <summary>
130136 /// Create the <see cref="OpenApiResponses"/> for a <see cref="IEdmOperation"/>
131137 /// </summary>
132138 /// <param name="context">The OData context.</param>
133139 /// <param name="operation">The Edm operation.</param>
134- /// <param name="path">The OData path.</param>
135140 /// <returns>The created <see cref="OpenApiResponses"/>.</returns>
136- public static OpenApiResponses CreateResponses ( this ODataContext context , IEdmOperation operation , ODataPath path )
141+ public static OpenApiResponses CreateResponses ( this ODataContext context , IEdmOperation operation )
137142 {
138143 Utils . CheckArgumentNull ( context , nameof ( context ) ) ;
139144 Utils . CheckArgumentNull ( operation , nameof ( operation ) ) ;
140- Utils . CheckArgumentNull ( path , nameof ( path ) ) ;
141145
142146 OpenApiResponses responses = new ( ) ;
143147
144148 if ( operation . IsAction ( ) && operation . ReturnType == null )
145149 {
146150 responses . Add ( Constants . StatusCode204 , Constants . StatusCode204 . GetResponse ( ) ) ;
147151 }
148- else
152+ else if ( context . Model . OperationTargetsMultiplePaths ( operation ) )
149153 {
150- OpenApiSchema schema ;
151- if ( operation . ReturnType . IsCollection ( ) )
152- {
153- // Get the entity type of the previous segment
154- IEdmEntityType entityType = path . Segments . Reverse ( ) . Skip ( 1 ) ? . Take ( 1 ) ? . FirstOrDefault ( ) ? . EntityType ;
155- schema = new OpenApiSchema
154+ responses . Add (
155+ context . Settings . UseSuccessStatusCodeRange ? Constants . StatusCodeClass2XX : Constants . StatusCode200 ,
156+ new OpenApiResponse
156157 {
157- Title = entityType == null ? null : $ "Collection of { entityType . Name } ",
158- Type = "object" ,
159- Properties = new Dictionary < string , OpenApiSchema >
158+ UnresolvedReference = true ,
159+ Reference = new OpenApiReference ( )
160160 {
161- {
162- "value" , context . CreateEdmTypeSchema ( operation . ReturnType )
163- }
161+ Type = ReferenceType . Response ,
162+ Id = $ "{ operation . Name } Response"
164163 }
165- } ;
166- }
167- else if ( operation . ReturnType . IsPrimitive ( ) )
164+ }
165+ ) ;
166+ }
167+ else
168+ {
169+ OpenApiResponse response = context . CreateOperationResponse ( operation ) ;
170+ responses . Add ( context . Settings . UseSuccessStatusCodeRange ? Constants . StatusCodeClass2XX : Constants . StatusCode200 , response ) ;
171+ }
172+
173+ if ( context . Settings . ErrorResponsesAsDefault )
174+ {
175+ responses . Add ( Constants . StatusCodeDefault , Constants . StatusCodeDefault . GetResponse ( ) ) ;
176+ }
177+ else
178+ {
179+ responses . Add ( Constants . StatusCodeClass4XX , Constants . StatusCodeClass4XX . GetResponse ( ) ) ;
180+ responses . Add ( Constants . StatusCodeClass5XX , Constants . StatusCodeClass5XX . GetResponse ( ) ) ;
181+ }
182+
183+ return responses ;
184+ }
185+
186+ public static OpenApiResponse CreateOperationResponse ( this ODataContext context , IEdmOperation operation )
187+ {
188+ if ( operation . ReturnType == null )
189+ return null ;
190+
191+ OpenApiSchema schema ;
192+ if ( operation . ReturnType . IsCollection ( ) )
193+ {
194+ schema = new OpenApiSchema
168195 {
169- // A property or operation response that is of a primitive type is represented as an object with a single name/value pair,
170- // whose name is value and whose value is a primitive value.
171- schema = new OpenApiSchema
196+ Title = operation . ReturnType . Definition . AsElementType ( ) is not IEdmEntityType entityType
197+ ? null : $ "Collection of { entityType . Name } ",
198+ Type = "object" ,
199+ Properties = new Dictionary < string , OpenApiSchema >
172200 {
173- Type = "object" ,
174- Properties = new Dictionary < string , OpenApiSchema >
175201 {
176- {
177- "value" , context . CreateEdmTypeSchema ( operation . ReturnType )
178- }
202+ "value" , context . CreateEdmTypeSchema ( operation . ReturnType )
179203 }
180- } ;
181- }
182- else
183- {
184- schema = context . CreateEdmTypeSchema ( operation . ReturnType ) ;
185- }
186-
187- string mediaType = Constants . ApplicationJsonMediaType ;
188- if ( operation . ReturnType . AsPrimitive ( ) ? . PrimitiveKind ( ) == EdmPrimitiveTypeKind . Stream )
189- {
190- // Responses of types Edm.Stream should be application/octet-stream
191- mediaType = Constants . ApplicationOctetStreamMediaType ;
192- }
193-
194- OpenApiResponse response = new ( )
204+ }
205+ } ;
206+ }
207+ else if ( operation . ReturnType . IsPrimitive ( ) )
208+ {
209+ // A property or operation response that is of a primitive type is represented as an object with a single name/value pair,
210+ // whose name is value and whose value is a primitive value.
211+ schema = new OpenApiSchema
195212 {
196- Description = "Success " ,
197- Content = new Dictionary < string , OpenApiMediaType >
213+ Type = "object " ,
214+ Properties = new Dictionary < string , OpenApiSchema >
198215 {
199216 {
200- mediaType ,
201- new OpenApiMediaType
202- {
203- Schema = schema
204- }
217+ "value" , context . CreateEdmTypeSchema ( operation . ReturnType )
205218 }
206219 }
207220 } ;
208- responses . Add ( context . Settings . UseSuccessStatusCodeRange ? Constants . StatusCodeClass2XX : Constants . StatusCode200 , response ) ;
209221 }
210-
211- if ( context . Settings . ErrorResponsesAsDefault )
222+ else
212223 {
213- responses . Add ( Constants . StatusCodeDefault , Constants . StatusCodeDefault . GetResponse ( ) ) ;
224+ schema = context . CreateEdmTypeSchema ( operation . ReturnType ) ;
214225 }
215- else
226+
227+ string mediaType = Constants . ApplicationJsonMediaType ;
228+ if ( operation . ReturnType . AsPrimitive ( ) ? . PrimitiveKind ( ) == EdmPrimitiveTypeKind . Stream )
216229 {
217- responses . Add ( Constants . StatusCodeClass4XX , Constants . StatusCodeClass4XX . GetResponse ( ) ) ;
218- responses . Add ( Constants . StatusCodeClass5XX , Constants . StatusCodeClass5XX . GetResponse ( ) ) ;
230+ // Responses of types Edm.Stream should be application/octet-stream
231+ mediaType = Constants . ApplicationOctetStreamMediaType ;
219232 }
220233
221- return responses ;
234+ OpenApiResponse response = new ( )
235+ {
236+ Description = "Success" ,
237+ Content = new Dictionary < string , OpenApiMediaType >
238+ {
239+ {
240+ mediaType ,
241+ new OpenApiMediaType
242+ {
243+ Schema = schema
244+ }
245+ }
246+ }
247+ } ;
248+
249+ return response ;
222250 }
223251
224252 private static OpenApiResponse CreateCollectionResponse ( IEdmStructuredType structuredType )
0 commit comments