OpenAPI String Enum Schema Erstellung mit .net 9C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 OpenAPI String Enum Schema Erstellung mit .net 9

Post by Anonymous »

Ich versuche, über OpenAPI in meinem ASP.NET Core 9 -Projekt eine ordnungsgemäße Enumerzeugung zu erhalten, damit ich ein TypeScript -Schema mit < /p>
erstellen kann

Code: Select all

npx openapi-typescript https://localhost:7088/openapi/v1.json -o ./apiSchema.ts --enum
< /code>
Da die Standard -OpenAPI -Implementierung in .NET 9 nur als Ganzzahlen Enums generiert, habe ich mich mit Schema -Transformatoren befasst. Allerdings habe ich nicht vollständig herausgefunden, wie man sie benutzt, noch hat Dokumente gefunden, die mir bei dem [url=viewtopic.php?t=15738]Problem[/url] geholfen haben, dass es das Schema verdoppelt. < /P>
var enumCache = new Dictionary();
options.AddSchemaTransformer(async delegate(
OpenApiSchema schema,
OpenApiSchemaTransformerContext context,
CancellationToken ct)
{
if (context?.JsonPropertyInfo?.PropertyType.IsEnum == true)
{
if (enumCache.TryGetValue(context.JsonPropertyInfo.PropertyType, out var enumSchema))
{
Console.WriteLine($"prop: {context?.JsonPropertyInfo?.Name}: enumSchema:{enumSchema} - from cache");
schema = enumSchema;
return;
}

var enumNames = Enum.GetNames(context.JsonPropertyInfo.PropertyType);
var enumValues = Enum.GetValues(context.JsonPropertyInfo.PropertyType);
var enumDesc = enumValues.Cast().Select(
(value, index) =>
{
// get description attribute via reflection
var attr = context.JsonPropertyInfo.PropertyType.GetField(value.ToString())?
.GetCustomAttribute();

return new
{
Value = (int) value,
Name = enumNames[index],
Description = attr?.Description
};
});

var openApiValueArray = new OpenApiArray();
var openApiNameArray = new OpenApiArray();
var openApiDescArray = new OpenApiArray();
foreach (var item in enumDesc)
{
openApiValueArray.Add(new OpenApiInteger(item.Value));
openApiNameArray.Add(new OpenApiString(item.Name));
openApiDescArray.Add(new OpenApiString(item.Description));
}

schema.Extensions.Add("x-enum-varnames", openApiNameArray); // https://openapi-ts.dev/advanced#enum-extensions
schema.Extensions.Add("x-enum-descriptions", openApiDescArray);
schema.Extensions.Add("enum", openApiValueArray);

enumCache[context.JsonPropertyInfo.PropertyType] = schema;

return;
}

return;
});
< /code>
Dies ist ein (partielles) Beispiel, bei dem die Typen doppelt sind. Dies betrifft auch alle enthaltenen Typen und Endpunkte und verwenden dann das falsche "alte" Schema. < /P>
"WorldStatus": {
"type": "integer",
"x-enum-varnames": [
"Active",
"Maintenance",
"Full",
"Ended"
],
"x-enum-descriptions": [
null,
null,
null,
"game over"
],
"enum": [
0,
1,
2,
3
]
},
"WorldStatus2": {
"type": "integer"
}
< /code>
TEST ENUM: < /p>
public enum WorldStatus
{
Active,
Maintenance,
Full,

[Description("game over")]
Ended
}
meine Frage ist also, wie Sie das Standard-/Originalschema entfernen. Ich habe das Gefühl, dass es einen einfacheren Weg geben muss und dann auch ein OperationTransformer erstelle, das jeden Typ und seine inneren Typen überprüft.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post