Skip to content

Commit c9f167f

Browse files
authored
Merge pull request #174 from microsoft/feature/error-naming
errors naming conventions improvements
2 parents 820c68e + 1c4e85c commit c9f167f

19 files changed

+166
-151
lines changed

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ namespace Microsoft.OpenApi.OData.Generator
1717
/// </summary>
1818
internal static class OpenApiErrorSchemaGenerator
1919
{
20+
internal const string ODataErrorClassName = "ODataError";
21+
internal const string MainErrorClassName = "MainError";
22+
internal const string ErrorDetailsClassName = "ErrorDetails";
23+
internal const string InnerErrorClassName = "InnerError";
24+
2025
/// <summary>
2126
/// Create the dictionary of <see cref="OpenApiSchema"/> object.
2227
/// The name of each pair is the namespace-qualified name of the type. It uses the namespace instead of the alias.
@@ -27,28 +32,36 @@ internal static class OpenApiErrorSchemaGenerator
2732
public static IDictionary<string, OpenApiSchema> CreateODataErrorSchemas(this ODataContext context)
2833
{
2934
Utils.CheckArgumentNull(context, nameof(context));
35+
var rootNamespaceName = context.GetErrorNamespaceName();
3036

3137
return new Dictionary<string, OpenApiSchema>()
3238
{
33-
// odata.error
34-
{ "odata.error", CreateErrorSchema() },
35-
36-
// odata.error.main
37-
{ "odata.error.main", CreateErrorMainSchema() },
38-
39-
// odata.error.detail
40-
{ "odata.error.detail", CreateErrorDetailSchema() },
41-
42-
// odata.error.innererror
43-
{ "odata.error.innererror", CreateInnerErrorSchema(context) }
39+
{ $"{rootNamespaceName}{ODataErrorClassName}", CreateErrorSchema(rootNamespaceName) },
40+
{ $"{rootNamespaceName}{MainErrorClassName}", CreateErrorMainSchema(rootNamespaceName) },
41+
{ $"{rootNamespaceName}{ErrorDetailsClassName}", CreateErrorDetailSchema() },
42+
{ $"{rootNamespaceName}{InnerErrorClassName}", CreateInnerErrorSchema(context) }
4443
};
4544
}
4645

4746
/// <summary>
48-
/// Create <see cref="OpenApiSchema"/> for "odata.error".
47+
/// Gets the error namespace name based on the root namespace of the model.
48+
/// </summary>
49+
/// <param name="context">The OData to Open API context.</param>
50+
/// <returns>The error namespace name.</returns>
51+
public static string GetErrorNamespaceName(this ODataContext context) {
52+
Utils.CheckArgumentNull(context, nameof(context));
53+
var rootNamespaceName = context.Model.DeclaredNamespaces.OrderBy(ns => ns.Count(y => y == '.')).FirstOrDefault();
54+
rootNamespaceName += (string.IsNullOrEmpty(rootNamespaceName) ? string.Empty : ".") +
55+
"ODataErrors.";
56+
return rootNamespaceName;
57+
}
58+
59+
/// <summary>
60+
/// Create <see cref="OpenApiSchema"/> for the error.
4961
/// </summary>
5062
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
51-
public static OpenApiSchema CreateErrorSchema()
63+
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
64+
public static OpenApiSchema CreateErrorSchema(string rootNamespaceName)
5265
{
5366
return new OpenApiSchema
5467
{
@@ -66,7 +79,7 @@ public static OpenApiSchema CreateErrorSchema()
6679
Reference = new OpenApiReference
6780
{
6881
Type = ReferenceType.Schema,
69-
Id = "odata.error.main"
82+
Id = $"{rootNamespaceName}{MainErrorClassName}"
7083
}
7184
}
7285
}
@@ -100,10 +113,11 @@ public static OpenApiSchema CreateInnerErrorSchema(ODataContext context)
100113
}
101114

102115
/// <summary>
103-
/// Create <see cref="OpenApiSchema"/> for "odata.error.main".
116+
/// Create <see cref="OpenApiSchema"/> for main property of the error.
104117
/// </summary>
118+
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
105119
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
106-
public static OpenApiSchema CreateErrorMainSchema()
120+
public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName)
107121
{
108122
return new OpenApiSchema
109123
{
@@ -133,7 +147,7 @@ public static OpenApiSchema CreateErrorMainSchema()
133147
Reference = new OpenApiReference
134148
{
135149
Type = ReferenceType.Schema,
136-
Id = "odata.error.detail"
150+
Id = $"{rootNamespaceName}{ErrorDetailsClassName}"
137151
}
138152
}
139153
}
@@ -145,7 +159,7 @@ public static OpenApiSchema CreateErrorMainSchema()
145159
Reference = new OpenApiReference
146160
{
147161
Type = ReferenceType.Schema,
148-
Id = "odata.error.innererror"
162+
Id = $"{rootNamespaceName}{InnerErrorClassName}"
149163
}
150164
}
151165
}
@@ -154,7 +168,7 @@ public static OpenApiSchema CreateErrorMainSchema()
154168
}
155169

156170
/// <summary>
157-
/// Create <see cref="OpenApiSchema"/> for "odata.error.detail".
171+
/// Create <see cref="OpenApiSchema"/> for detail property of the error.
158172
/// </summary>
159173
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
160174
public static OpenApiSchema CreateErrorDetailSchema()

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static IDictionary<string, OpenApiResponse> CreateResponses(this ODataCon
8181

8282
var responses = new Dictionary<string, OpenApiResponse>
8383
{
84-
{ "error", CreateErrorResponse() }
84+
{ "error", context.CreateErrorResponse() }
8585
};
8686

8787
if(context.Settings.EnableDollarCountPath)
@@ -259,8 +259,9 @@ private static OpenApiResponse CreateCountResponse()
259259
};
260260
}
261261

262-
private static OpenApiResponse CreateErrorResponse()
262+
private static OpenApiResponse CreateErrorResponse(this ODataContext context)
263263
{
264+
var errorNamespaceName = context.GetErrorNamespaceName();
264265
return new OpenApiResponse
265266
{
266267
Description = "error",
@@ -275,7 +276,7 @@ private static OpenApiResponse CreateErrorResponse()
275276
Reference = new OpenApiReference
276277
{
277278
Type = ReferenceType.Schema,
278-
Id = "odata.error"
279+
Id = $"{errorNamespaceName}{OpenApiErrorSchemaGenerator.ODataErrorClassName}"
279280
}
280281
}
281282
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiResponseGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void CanSerializeAsJsonFromTheCreatedResponses()
113113
""content"": {
114114
""application/json"": {
115115
""schema"": {
116-
""$ref"": ""#/components/schemas/odata.error""
116+
""$ref"": ""#/components/schemas/ODataErrors.ODataError""
117117
}
118118
}
119119
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,18 +1872,18 @@
18721872
}
18731873
}
18741874
},
1875-
"odata.error": {
1875+
"DefaultNs.ODataErrors.ODataError": {
18761876
"required": [
18771877
"error"
18781878
],
18791879
"type": "object",
18801880
"properties": {
18811881
"error": {
1882-
"$ref": "#/definitions/odata.error.main"
1882+
"$ref": "#/definitions/DefaultNs.ODataErrors.MainError"
18831883
}
18841884
}
18851885
},
1886-
"odata.error.main": {
1886+
"DefaultNs.ODataErrors.MainError": {
18871887
"required": [
18881888
"code",
18891889
"message"
@@ -1902,15 +1902,15 @@
19021902
"details": {
19031903
"type": "array",
19041904
"items": {
1905-
"$ref": "#/definitions/odata.error.detail"
1905+
"$ref": "#/definitions/DefaultNs.ODataErrors.ErrorDetails"
19061906
}
19071907
},
19081908
"innererror": {
1909-
"$ref": "#/definitions/odata.error.innererror"
1909+
"$ref": "#/definitions/DefaultNs.ODataErrors.InnerError"
19101910
}
19111911
}
19121912
},
1913-
"odata.error.detail": {
1913+
"DefaultNs.ODataErrors.ErrorDetails": {
19141914
"required": [
19151915
"code",
19161916
"message"
@@ -1928,7 +1928,7 @@
19281928
}
19291929
}
19301930
},
1931-
"odata.error.innererror": {
1931+
"DefaultNs.ODataErrors.InnerError": {
19321932
"description": "The structure of this object is service-specific",
19331933
"type": "object"
19341934
},
@@ -2023,7 +2023,7 @@
20232023
"error": {
20242024
"description": "error",
20252025
"schema": {
2026-
"$ref": "#/definitions/odata.error"
2026+
"$ref": "#/definitions/DefaultNs.ODataErrors.ODataError"
20272027
}
20282028
},
20292029
"ODataCountResponse": {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,14 +1237,14 @@ definitions:
12371237
'@odata.type': DefaultNs.City
12381238
CountryOrRegion:
12391239
'@odata.type': DefaultNs.CountryOrRegion
1240-
odata.error:
1240+
DefaultNs.ODataErrors.ODataError:
12411241
required:
12421242
- error
12431243
type: object
12441244
properties:
12451245
error:
1246-
$ref: '#/definitions/odata.error.main'
1247-
odata.error.main:
1246+
$ref: '#/definitions/DefaultNs.ODataErrors.MainError'
1247+
DefaultNs.ODataErrors.MainError:
12481248
required:
12491249
- code
12501250
- message
@@ -1259,10 +1259,10 @@ definitions:
12591259
details:
12601260
type: array
12611261
items:
1262-
$ref: '#/definitions/odata.error.detail'
1262+
$ref: '#/definitions/DefaultNs.ODataErrors.ErrorDetails'
12631263
innererror:
1264-
$ref: '#/definitions/odata.error.innererror'
1265-
odata.error.detail:
1264+
$ref: '#/definitions/DefaultNs.ODataErrors.InnerError'
1265+
DefaultNs.ODataErrors.ErrorDetails:
12661266
required:
12671267
- code
12681268
- message
@@ -1274,7 +1274,7 @@ definitions:
12741274
type: string
12751275
target:
12761276
type: string
1277-
odata.error.innererror:
1277+
DefaultNs.ODataErrors.InnerError:
12781278
description: The structure of this object is service-specific
12791279
type: object
12801280
ODataCountResponse:
@@ -1344,7 +1344,7 @@ responses:
13441344
error:
13451345
description: error
13461346
schema:
1347-
$ref: '#/definitions/odata.error'
1347+
$ref: '#/definitions/DefaultNs.ODataErrors.ODataError'
13481348
ODataCountResponse:
13491349
description: The count of the resource
13501350
schema:

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,18 +2103,18 @@
21032103
}
21042104
}
21052105
},
2106-
"odata.error": {
2106+
"DefaultNs.ODataErrors.ODataError": {
21072107
"required": [
21082108
"error"
21092109
],
21102110
"type": "object",
21112111
"properties": {
21122112
"error": {
2113-
"$ref": "#/components/schemas/odata.error.main"
2113+
"$ref": "#/components/schemas/DefaultNs.ODataErrors.MainError"
21142114
}
21152115
}
21162116
},
2117-
"odata.error.main": {
2117+
"DefaultNs.ODataErrors.MainError": {
21182118
"required": [
21192119
"code",
21202120
"message"
@@ -2134,15 +2134,15 @@
21342134
"details": {
21352135
"type": "array",
21362136
"items": {
2137-
"$ref": "#/components/schemas/odata.error.detail"
2137+
"$ref": "#/components/schemas/DefaultNs.ODataErrors.ErrorDetails"
21382138
}
21392139
},
21402140
"innererror": {
2141-
"$ref": "#/components/schemas/odata.error.innererror"
2141+
"$ref": "#/components/schemas/DefaultNs.ODataErrors.InnerError"
21422142
}
21432143
}
21442144
},
2145-
"odata.error.detail": {
2145+
"DefaultNs.ODataErrors.ErrorDetails": {
21462146
"required": [
21472147
"code",
21482148
"message"
@@ -2161,7 +2161,7 @@
21612161
}
21622162
}
21632163
},
2164-
"odata.error.innererror": {
2164+
"DefaultNs.ODataErrors.InnerError": {
21652165
"type": "object",
21662166
"description": "The structure of this object is service-specific"
21672167
},
@@ -2224,7 +2224,7 @@
22242224
"content": {
22252225
"application/json": {
22262226
"schema": {
2227-
"$ref": "#/components/schemas/odata.error"
2227+
"$ref": "#/components/schemas/DefaultNs.ODataErrors.ODataError"
22282228
}
22292229
}
22302230
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,14 +1395,14 @@ components:
13951395
'@odata.type': DefaultNs.City
13961396
CountryOrRegion:
13971397
'@odata.type': DefaultNs.CountryOrRegion
1398-
odata.error:
1398+
DefaultNs.ODataErrors.ODataError:
13991399
required:
14001400
- error
14011401
type: object
14021402
properties:
14031403
error:
1404-
$ref: '#/components/schemas/odata.error.main'
1405-
odata.error.main:
1404+
$ref: '#/components/schemas/DefaultNs.ODataErrors.MainError'
1405+
DefaultNs.ODataErrors.MainError:
14061406
required:
14071407
- code
14081408
- message
@@ -1418,10 +1418,10 @@ components:
14181418
details:
14191419
type: array
14201420
items:
1421-
$ref: '#/components/schemas/odata.error.detail'
1421+
$ref: '#/components/schemas/DefaultNs.ODataErrors.ErrorDetails'
14221422
innererror:
1423-
$ref: '#/components/schemas/odata.error.innererror'
1424-
odata.error.detail:
1423+
$ref: '#/components/schemas/DefaultNs.ODataErrors.InnerError'
1424+
DefaultNs.ODataErrors.ErrorDetails:
14251425
required:
14261426
- code
14271427
- message
@@ -1434,7 +1434,7 @@ components:
14341434
target:
14351435
type: string
14361436
nullable: true
1437-
odata.error.innererror:
1437+
DefaultNs.ODataErrors.InnerError:
14381438
type: object
14391439
description: The structure of this object is service-specific
14401440
ODataCountResponse:
@@ -1478,7 +1478,7 @@ components:
14781478
content:
14791479
application/json:
14801480
schema:
1481-
$ref: '#/components/schemas/odata.error'
1481+
$ref: '#/components/schemas/DefaultNs.ODataErrors.ODataError'
14821482
ODataCountResponse:
14831483
description: The count of the resource
14841484
content:

0 commit comments

Comments
 (0)