{"id":15091289,"url":"https://github.com/alexscigalszky/openapi.errorcodes.library","last_synced_at":"2026-02-17T22:02:37.915Z","repository":{"id":190434622,"uuid":"682270973","full_name":"AlexScigalszky/OpenApi.ErrorCodes.Library","owner":"AlexScigalszky","description":"This is a library to show examples of error codes from a constants file to a defined structure","archived":false,"fork":false,"pushed_at":"2023-08-24T16:05:56.000Z","size":49,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T10:48:04.524Z","etag":null,"topics":["csharp","openapi-generator","openapi-spec","openapi-specification","openapi3","swagger","swagger-ui"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexScigalszky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-23T20:11:26.000Z","updated_at":"2023-08-24T16:08:26.000Z","dependencies_parsed_at":"2023-08-24T17:13:28.240Z","dependency_job_id":null,"html_url":"https://github.com/AlexScigalszky/OpenApi.ErrorCodes.Library","commit_stats":null,"previous_names":["alexscigalszky/openapi.errorcodes.library"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexScigalszky%2FOpenApi.ErrorCodes.Library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexScigalszky%2FOpenApi.ErrorCodes.Library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexScigalszky%2FOpenApi.ErrorCodes.Library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexScigalszky%2FOpenApi.ErrorCodes.Library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexScigalszky","download_url":"https://codeload.github.com/AlexScigalszky/OpenApi.ErrorCodes.Library/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244945594,"owners_count":20536296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","openapi-generator","openapi-spec","openapi-specification","openapi3","swagger","swagger-ui"],"created_at":"2024-09-25T10:36:50.002Z","updated_at":"2025-10-17T16:36:02.504Z","avatar_url":"https://github.com/AlexScigalszky.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenApi.ErrorCodes.Library\n\nThis is a library to show examples of error codes from a constants file to a defined structure\n\n\n## How to use\n* Create an attribute extending ´ResponseCodeFromConstantsDataAttribute´ to save your data.\nExample\n```csharp\n[AttributeUsage(AttributeTargets.Field)]\npublic class CodeSubCodeDescriptionLinkAttribute : ResponseCodeFromConstantsDataAttribute\n{\n    public int Code;\n    public int? SubCode;\n    public string Description;\n    public string Link;\n    public CodeSubCodeDescriptionLinkAttribute(int code, string description, string link)\n    {\n        Code = code;\n        SubCode = null;\n        Description = description;\n        Link = link;\n    }\n}\n```\n\n* Create a Response class\nExample\n\n```csharp\npublic class CodeSubCodeResponse\n{\n    public int Code { get; set; }\n    public int SubCode { get; set; }\n    public string? Description { get; set; }\n    public string? Link { get; set; }\n}\n```\n\n* Create a mapping function\nExample\n```csharp\nvar MapAttributeToResponse = (CodeSubCodeDescriptionLinkAttribute data, int constant) =\u003e\n{\n    return new CodeSubCodeResponse()\n    {\n        Code = data.Code,\n        SubCode = constant,\n        Description = data.Description,\n        Link = data.Link\n    };\n};\n```\n\n* Add the OpenApi filter in your startup project\n```csharp\nbuilder.Services.AddEndpointsApiExplorer();\nbuilder.Services.AddSwaggerGen(c =\u003e\n{\n    c.SwaggerDoc(\"v1\", new()\n    {\n        Title = builder.Environment.ApplicationName,\n        Version = \"v1\",\n    });\n    /// add next line\n    c.OperationFilter\u003cResponseCodeFromConstantsOperationFilter\u003cCodeSubCodeResponse, CodeSubCodeDescriptionLinkAttribute\u003e\u003e(MapAttributeToResponse);\n});\n```\n\n* Add attributo in your endpoint function. \nIt needs a section name and the class where the constant codes are defined\nExample\n```csharp\nvar HomeFn = \n    // add next line\n    [ResponseCodeFromConstants(\"List of error codes\", typeof(ResponseSubCodes))]\n([FromBody] InputRequest input) =\u003e {\n\n    return \"Hello word\";\n};\n```\n\n## Example of a Swagger UI\n\n\u003cimg src=\"swaggerui.png\"/\u003e\n\n## Example of a OpenApi json file\n\n```json\n{\n  \"openapi\": \"3.0.1\",\n  \"info\": {\n    \"title\": \"OpenApi.ErrorCodes.Library.Console\",\n    \"version\": \"v1\"\n  },\n  \"paths\": {\n    \"/\": {\n      \"post\": {\n        \"tags\": [\n          \"OpenApi.ErrorCodes.Library.Console\"\n        ],\n        \"requestBody\": {\n          \"content\": {\n            \"application/json\": {\n              \"schema\": {\n                \"$ref\": \"#/components/schemas/InputRequest\"\n              }\n            }\n          },\n          \"required\": true\n        },\n        \"responses\": {\n          \"200\": {\n            \"description\": \"Success\",\n            \"content\": {\n              \"text/plain\": {\n                \"schema\": {\n                  \"type\": \"string\"\n                }\n              }\n            }\n          },\n          \"List of error codes\": {\n            \"description\": null,\n            \"content\": {\n              \"application/json\": {\n                \"schema\": {\n                  \"$ref\": \"#/components/schemas/CodeSubCodeResponse\"\n                },\n                \"examples\": {\n                  \"1031\": {\n                    \"value\": \"{\\\"Code\\\":200,\\\"SubCode\\\":1031,\\\"Description\\\":\\\"Item created!\\\",\\\"Link\\\":\\\"http://tbd\\\"}\"\n                  },\n                  \"2531\": {\n                    \"value\": \"{\\\"Code\\\":200,\\\"SubCode\\\":2531,\\\"Description\\\":\\\"Item updated!\\\",\\\"Link\\\":\\\"http://tbd\\\"}\"\n                  },\n                  \"9054\": {\n                    \"value\": \"{\\\"Code\\\":300,\\\"SubCode\\\":9054,\\\"Description\\\":\\\"A field is missing\\\",\\\"Link\\\":\\\"http://tbd\\\"}\"\n                  }\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  },\n  \"components\": {\n    \"schemas\": {\n      \"CodeSubCodeResponse\": {\n        \"type\": \"object\",\n        \"properties\": {\n          \"code\": {\n            \"type\": \"integer\",\n            \"format\": \"int32\"\n          },\n          \"subCode\": {\n            \"type\": \"integer\",\n            \"format\": \"int32\"\n          },\n          \"description\": {\n            \"type\": \"string\",\n            \"nullable\": true\n          },\n          \"link\": {\n            \"type\": \"string\",\n            \"nullable\": true\n          }\n        },\n        \"additionalProperties\": false\n      },\n      \"InputRequest\": {\n        \"type\": \"object\",\n        \"properties\": {\n          \"name\": {\n            \"type\": \"string\",\n            \"nullable\": true\n          },\n          \"count\": {\n            \"type\": \"integer\",\n            \"format\": \"int32\",\n            \"nullable\": true\n          }\n        },\n        \"additionalProperties\": false\n      }\n    }\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexscigalszky%2Fopenapi.errorcodes.library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexscigalszky%2Fopenapi.errorcodes.library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexscigalszky%2Fopenapi.errorcodes.library/lists"}