Skip to content

Commit 313ee7a

Browse files
committed
fix flaky behaviour with error responses
Signed-off-by: Vincent Biret <[email protected]>
1 parent d8c25f3 commit 313ee7a

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiResponseGenerator.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
67
using System.Collections.Generic;
78
using System.Diagnostics.CodeAnalysis;
89
using System.Linq;
@@ -21,25 +22,6 @@ namespace Microsoft.OpenApi.OData.Generator
2122
/// </summary>
2223
internal static class OpenApiResponseGenerator
2324
{
24-
private static Dictionary<string, IOpenApiResponse> _responses;
25-
26-
private static Dictionary<string, IOpenApiResponse> GetResponses(OpenApiDocument openApiDocument)
27-
{
28-
_responses ??= new()
29-
{
30-
{ Constants.StatusCodeDefault,
31-
new OpenApiResponseReference(Constants.Error, openApiDocument)
32-
},
33-
34-
{ Constants.StatusCode204, new OpenApiResponse { Description = Constants.Success} },
35-
{ Constants.StatusCode201, new OpenApiResponse { Description = Constants.Created} },
36-
{ Constants.StatusCodeClass2XX, new OpenApiResponse { Description = Constants.Success} },
37-
{ Constants.StatusCodeClass4XX, new OpenApiResponseReference(Constants.Error, openApiDocument)},
38-
{ Constants.StatusCodeClass5XX, new OpenApiResponseReference(Constants.Error, openApiDocument)}
39-
};
40-
return _responses;
41-
}
42-
4325
/// <summary>
4426
/// Get the <see cref="IOpenApiResponse"/> for the build-in statusCode.
4527
/// </summary>
@@ -48,12 +30,15 @@ private static Dictionary<string, IOpenApiResponse> GetResponses(OpenApiDocument
4830
/// <returns>The created <see cref="IOpenApiResponse"/>.</returns>
4931
public static IOpenApiResponse GetResponse(this string statusCode, OpenApiDocument document)
5032
{
51-
if (GetResponses(document).TryGetValue(statusCode, out var response))
52-
{
53-
return response;
54-
}
55-
56-
return null;
33+
return statusCode switch {
34+
Constants.StatusCodeDefault => new OpenApiResponseReference(Constants.Error, document),
35+
Constants.StatusCode204 => new OpenApiResponse { Description = Constants.Success},
36+
Constants.StatusCode201 => new OpenApiResponse { Description = Constants.Created},
37+
Constants.StatusCodeClass2XX => new OpenApiResponse { Description = Constants.Success},
38+
Constants.StatusCodeClass4XX => new OpenApiResponseReference(Constants.Error, document),
39+
Constants.StatusCodeClass5XX => new OpenApiResponseReference(Constants.Error, document),
40+
_ => null,
41+
};
5742
}
5843

5944
/// <summary>

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ paths:
122122
operationId: City.City.UpdateCity
123123
consumes:
124124
- application/json
125+
produces:
126+
- application/json
125127
parameters:
126128
- in: path
127129
name: Name
@@ -146,6 +148,8 @@ paths:
146148
- City.City
147149
summary: Delete entity from City
148150
operationId: City.City.DeleteCity
151+
produces:
152+
- application/json
149153
parameters:
150154
- in: path
151155
name: Name
@@ -172,6 +176,7 @@ paths:
172176
operationId: City.GetCount-8728
173177
produces:
174178
- text/plain
179+
- application/json
175180
parameters:
176181
- $ref: '#/parameters/search'
177182
- $ref: '#/parameters/filter'
@@ -296,6 +301,8 @@ paths:
296301
operationId: CountryOrRegion.CountryOrRegion.UpdateCountryOrRegion
297302
consumes:
298303
- application/json
304+
produces:
305+
- application/json
299306
parameters:
300307
- in: path
301308
name: Name
@@ -320,6 +327,8 @@ paths:
320327
- CountryOrRegion.CountryOrRegion
321328
summary: Delete entity from CountryOrRegion
322329
operationId: CountryOrRegion.CountryOrRegion.DeleteCountryOrRegion
330+
produces:
331+
- application/json
323332
parameters:
324333
- in: path
325334
name: Name
@@ -346,6 +355,7 @@ paths:
346355
operationId: CountryOrRegion.GetCount-daf5
347356
produces:
348357
- text/plain
358+
- application/json
349359
parameters:
350360
- $ref: '#/parameters/search'
351361
- $ref: '#/parameters/filter'
@@ -398,6 +408,8 @@ paths:
398408
operationId: Me.Person.UpdatePerson
399409
consumes:
400410
- application/json
411+
produces:
412+
- application/json
401413
parameters:
402414
- in: body
403415
name: body
@@ -540,6 +552,8 @@ paths:
540552
operationId: People.Person.UpdatePerson
541553
consumes:
542554
- application/json
555+
produces:
556+
- application/json
543557
parameters:
544558
- in: path
545559
name: UserName
@@ -564,6 +578,8 @@ paths:
564578
- People.Person
565579
summary: Delete entity from People
566580
operationId: People.Person.DeletePerson
581+
produces:
582+
- application/json
567583
parameters:
568584
- in: path
569585
name: UserName
@@ -590,6 +606,7 @@ paths:
590606
operationId: People.GetCount-dd8d
591607
produces:
592608
- text/plain
609+
- application/json
593610
parameters:
594611
- $ref: '#/parameters/search'
595612
- $ref: '#/parameters/filter'

0 commit comments

Comments
 (0)