OpenAPI -Parameter mit VererbungC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 OpenAPI -Parameter mit Vererbung

Post by Anonymous »

Ich versuche, eine .NET -Framework -WCF -Anwendung in eine .NET 9 OpenAPI -Anwendung zu modernisieren.

Code: Select all

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
In der StandardwetterforeCastController habe ich diese Methode hinzugefügt:

Code: Select all

[HttpGet("vehicle", Name = "GetVehicle")]
public RequestVehicle Vehicle([FromBody] RequestVehicle requestVehicle)
{
requestVehicle.Vehicle.Start();
return requestVehicle;
}
< /code>
, der diese Klassen verwendet: < /p>
public class RequestVehicle
{
public Vehicle Vehicle { get; set; }
}

public abstract class Vehicle
{
public string Status { get; set; }
public string Name { get; set;  }
public abstract void Start();

protected Vehicle()
{
Status = "Not running";
}
}

public class Car : Vehicle
{
public Car()
{
Name = "Car";
}

public override void Start()
{
Console.WriteLine("Broemmm pruttle put");
Status = "Running";
}
}
< /code>
Wenn ich die Anwendung ausführe und zu: < /p>
gehehttps://localhost:7262/openapi/v1.json
< /code>
Ich erhalte diesen JSON: < /p>
{
"openapi": "3.0.1",
"info": {
"title": "WebApplication3 | v1",
"version": "1.0.0"
},
"servers": [
{
"url": "https://localhost:7262"
},
{
"url": "http://localhost:5136"
}
],
"paths": {
"/WeatherForecast": {
"get": {
"tags": [
"WeatherForecast"
],
"operationId": "GetWeatherForecast",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
},
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
},
"text/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
}
}
}
}
}
},
"/WeatherForecast/vehicle": {
"get": {
"tags": [
"WeatherForecast"
],
"operationId": "GetVehicle",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/RequestVehicle"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"RequestVehicle": {
"type": "object",
"properties": {
"vehicle": {
"$ref": "#/components/schemas/Vehicle"
}
}
},
"Vehicle":  {
"type": "object",
"properties": {
"status": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"WeatherForecast": {
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date"
},
"temperatureC": {
"type": "integer",
"format": "int32"
},
"temperatureF": {
"type": "integer",
"format": "int32"
},
"summary": {
"type": "string",
"nullable": true
}
}
}
}
},
"tags": [
{
"name": "WeatherForecast"
}
]
}
< /code>
Verwenden dieses JSON, erstelle ich den Client mit NSWAGStudio. < /p>
Hier ist ein Teil des generierten Code:[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class RequestVehicle
{
[Newtonsoft.Json.JsonProperty("vehicle", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Vehicle Vehicle { get; set; }

private System.Collections.Generic.IDictionary _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.IDictionary AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
set { _additionalProperties = value; }
}
}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Vehicle
{
[Newtonsoft.Json.JsonProperty("status", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Status { get; set; }

[Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Name { get; set; }

private System.Collections.Generic.IDictionary _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.IDictionary AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }
set { _additionalProperties = value; }
}
}
Das Problem ist, dass das Auto Objekt im generierten Code in NSWAGSTUDIO fehlt. Ich habe mehrere Ansätze mit ChatGPT ausprobiert, aber es scheint, dass ich dem Fahrzeugobjekt explizit sagen muss, dass es sich auch um ein Auto .
Ich habe mehrere Vorschläge ausprobiert, aber ich kann es nicht zum Laufen bringen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post