Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors naming conventions improvements #174

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,36 @@ internal static class OpenApiErrorSchemaGenerator
public static IDictionary<string, OpenApiSchema> CreateODataErrorSchemas(this ODataContext context)
{
Utils.CheckArgumentNull(context, nameof(context));
var rootNamespaceName = context.GetErrorNamespaceName();

return new Dictionary<string, OpenApiSchema>()
{
// odata.error
{ "odata.error", CreateErrorSchema() },

// odata.error.main
{ "odata.error.main", CreateErrorMainSchema() },

// odata.error.detail
{ "odata.error.detail", CreateErrorDetailSchema() },

// odata.error.innererror
{ "odata.error.innererror", CreateInnerErrorSchema(context) }
{ $"{rootNamespaceName}odataerror", CreateErrorSchema(rootNamespaceName) },
baywet marked this conversation as resolved.
Show resolved Hide resolved
{ $"{rootNamespaceName}mainerror", CreateErrorMainSchema(rootNamespaceName) },
{ $"{rootNamespaceName}errordetails", CreateErrorDetailSchema() },
{ $"{rootNamespaceName}innererror", CreateInnerErrorSchema(context) }
};
}

/// <summary>
/// Create <see cref="OpenApiSchema"/> for "odata.error".
/// Gets the error namespace name based on the root namespace of the model.
/// </summary>
/// <param name="context">The OData to Open API context.</param>
/// <returns>The error namespace name.</returns>
public static string GetErrorNamespaceName(this ODataContext context) {
Utils.CheckArgumentNull(context, nameof(context));
var rootNamespaceName = context.Model.DeclaredNamespaces.OrderBy(ns => ns.Count(y => y == '.')).FirstOrDefault();
rootNamespaceName += (string.IsNullOrEmpty(rootNamespaceName) ? string.Empty : ".") +
"odataerrors.";
return rootNamespaceName;
}

/// <summary>
/// Create <see cref="OpenApiSchema"/> for the error.
/// </summary>
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
public static OpenApiSchema CreateErrorSchema()
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
public static OpenApiSchema CreateErrorSchema(string rootNamespaceName)
{
return new OpenApiSchema
{
Expand All @@ -66,7 +74,7 @@ public static OpenApiSchema CreateErrorSchema()
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "odata.error.main"
Id = $"{rootNamespaceName}mainerror"
}
}
}
Expand Down Expand Up @@ -100,10 +108,11 @@ public static OpenApiSchema CreateInnerErrorSchema(ODataContext context)
}

/// <summary>
/// Create <see cref="OpenApiSchema"/> for "odata.error.main".
/// Create <see cref="OpenApiSchema"/> for main property of the error.
/// </summary>
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
public static OpenApiSchema CreateErrorMainSchema()
public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName)
{
return new OpenApiSchema
{
Expand Down Expand Up @@ -133,7 +142,7 @@ public static OpenApiSchema CreateErrorMainSchema()
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "odata.error.detail"
Id = $"{rootNamespaceName}errordetails"
}
}
}
Expand All @@ -145,7 +154,7 @@ public static OpenApiSchema CreateErrorMainSchema()
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "odata.error.innererror"
Id = $"{rootNamespaceName}innererror"
}
}
}
Expand All @@ -154,7 +163,7 @@ public static OpenApiSchema CreateErrorMainSchema()
}

/// <summary>
/// Create <see cref="OpenApiSchema"/> for "odata.error.detail".
/// Create <see cref="OpenApiSchema"/> for detail property of the error.
/// </summary>
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
public static OpenApiSchema CreateErrorDetailSchema()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static IDictionary<string, OpenApiResponse> CreateResponses(this ODataCon

var responses = new Dictionary<string, OpenApiResponse>
{
{ "error", CreateErrorResponse() }
{ "error", context.CreateErrorResponse() }
};

if(context.Settings.EnableDollarCountPath)
Expand Down Expand Up @@ -259,8 +259,9 @@ private static OpenApiResponse CreateCountResponse()
};
}

private static OpenApiResponse CreateErrorResponse()
private static OpenApiResponse CreateErrorResponse(this ODataContext context)
{
var errorNamespaceName = context.GetErrorNamespaceName();
return new OpenApiResponse
{
Description = "error",
Expand All @@ -275,7 +276,7 @@ private static OpenApiResponse CreateErrorResponse()
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "odata.error"
Id = $"{errorNamespaceName}odataerror"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void CanSerializeAsJsonFromTheCreatedResponses()
""content"": {
""application/json"": {
""schema"": {
""$ref"": ""#/components/schemas/odata.error""
""$ref"": ""#/components/schemas/odataerrors.odataerror""
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2240,18 +2240,18 @@
}
}
},
"odata.error": {
"DefaultNs.odataerrors.odataerror": {
"required": [
"error"
],
"type": "object",
"properties": {
"error": {
"$ref": "#/definitions/odata.error.main"
"$ref": "#/definitions/DefaultNs.odataerrors.mainerror"
}
}
},
"odata.error.main": {
"DefaultNs.odataerrors.mainerror": {
"required": [
"code",
"message"
Expand All @@ -2270,15 +2270,15 @@
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/odata.error.detail"
"$ref": "#/definitions/DefaultNs.odataerrors.errordetails"
}
},
"innererror": {
"$ref": "#/definitions/odata.error.innererror"
"$ref": "#/definitions/DefaultNs.odataerrors.innererror"
}
}
},
"odata.error.detail": {
"DefaultNs.odataerrors.errordetails": {
"required": [
"code",
"message"
Expand All @@ -2296,7 +2296,7 @@
}
}
},
"odata.error.innererror": {
"DefaultNs.odataerrors.innererror": {
"description": "The structure of this object is service-specific",
"type": "object"
},
Expand Down Expand Up @@ -2391,7 +2391,7 @@
"error": {
"description": "error",
"schema": {
"$ref": "#/definitions/odata.error"
"$ref": "#/definitions/DefaultNs.odataerrors.odataerror"
}
},
"ODataCountResponse": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,14 +1487,14 @@ definitions:
'@odata.type': DefaultNs.City
CountryOrRegion:
'@odata.type': DefaultNs.CountryOrRegion
odata.error:
DefaultNs.odataerrors.odataerror:
required:
- error
type: object
properties:
error:
$ref: '#/definitions/odata.error.main'
odata.error.main:
$ref: '#/definitions/DefaultNs.odataerrors.mainerror'
DefaultNs.odataerrors.mainerror:
required:
- code
- message
Expand All @@ -1509,10 +1509,10 @@ definitions:
details:
type: array
items:
$ref: '#/definitions/odata.error.detail'
$ref: '#/definitions/DefaultNs.odataerrors.errordetails'
innererror:
$ref: '#/definitions/odata.error.innererror'
odata.error.detail:
$ref: '#/definitions/DefaultNs.odataerrors.innererror'
DefaultNs.odataerrors.errordetails:
required:
- code
- message
Expand All @@ -1524,7 +1524,7 @@ definitions:
type: string
target:
type: string
odata.error.innererror:
DefaultNs.odataerrors.innererror:
description: The structure of this object is service-specific
type: object
ODataCountResponse:
Expand Down Expand Up @@ -1594,7 +1594,7 @@ responses:
error:
description: error
schema:
$ref: '#/definitions/odata.error'
$ref: '#/definitions/DefaultNs.odataerrors.odataerror'
ODataCountResponse:
description: The count of the resource
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2487,18 +2487,18 @@
}
}
},
"odata.error": {
"DefaultNs.odataerrors.odataerror": {
"required": [
"error"
],
"type": "object",
"properties": {
"error": {
"$ref": "#/components/schemas/odata.error.main"
"$ref": "#/components/schemas/DefaultNs.odataerrors.mainerror"
}
}
},
"odata.error.main": {
"DefaultNs.odataerrors.mainerror": {
"required": [
"code",
"message"
Expand All @@ -2518,15 +2518,15 @@
"details": {
"type": "array",
"items": {
"$ref": "#/components/schemas/odata.error.detail"
"$ref": "#/components/schemas/DefaultNs.odataerrors.errordetails"
}
},
"innererror": {
"$ref": "#/components/schemas/odata.error.innererror"
"$ref": "#/components/schemas/DefaultNs.odataerrors.innererror"
}
}
},
"odata.error.detail": {
"DefaultNs.odataerrors.errordetails": {
"required": [
"code",
"message"
Expand All @@ -2545,7 +2545,7 @@
}
}
},
"odata.error.innererror": {
"DefaultNs.odataerrors.innererror": {
"type": "object",
"description": "The structure of this object is service-specific"
},
Expand Down Expand Up @@ -2608,7 +2608,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/odata.error"
"$ref": "#/components/schemas/DefaultNs.odataerrors.odataerror"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1649,14 +1649,14 @@ components:
'@odata.type': DefaultNs.City
CountryOrRegion:
'@odata.type': DefaultNs.CountryOrRegion
odata.error:
DefaultNs.odataerrors.odataerror:
required:
- error
type: object
properties:
error:
$ref: '#/components/schemas/odata.error.main'
odata.error.main:
$ref: '#/components/schemas/DefaultNs.odataerrors.mainerror'
DefaultNs.odataerrors.mainerror:
required:
- code
- message
Expand All @@ -1672,10 +1672,10 @@ components:
details:
type: array
items:
$ref: '#/components/schemas/odata.error.detail'
$ref: '#/components/schemas/DefaultNs.odataerrors.errordetails'
innererror:
$ref: '#/components/schemas/odata.error.innererror'
odata.error.detail:
$ref: '#/components/schemas/DefaultNs.odataerrors.innererror'
DefaultNs.odataerrors.errordetails:
required:
- code
- message
Expand All @@ -1688,7 +1688,7 @@ components:
target:
type: string
nullable: true
odata.error.innererror:
DefaultNs.odataerrors.innererror:
type: object
description: The structure of this object is service-specific
ODataCountResponse:
Expand Down Expand Up @@ -1732,7 +1732,7 @@ components:
content:
application/json:
schema:
$ref: '#/components/schemas/odata.error'
$ref: '#/components/schemas/DefaultNs.odataerrors.odataerror'
ODataCountResponse:
description: The count of the resource
content:
Expand Down
Loading