mirror of
https://github.com/unclshura/BlazorOpenApi.git
synced 2025-12-21 09:51:53 +00:00
Minor nuget update. Example code generation.
This commit is contained in:
parent
959fef4418
commit
a01262ff8a
@ -51,6 +51,11 @@
|
||||
<ParametersControl Value="@Value.Parameters"/>
|
||||
<RequestBodyControl Value="@Value.RequestBody"/>
|
||||
<ResponsesControl Value="@Value.Responses" />
|
||||
|
||||
<div class="example-data">
|
||||
<h3>Example Data</h3>
|
||||
<pre>@GenerateExampleData()</pre>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@ -73,4 +78,90 @@
|
||||
_expanded = !_expanded;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private string GenerateExampleData()
|
||||
{
|
||||
if (Value == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var exampleData = new Dictionary<string, object>();
|
||||
|
||||
// Generate example data for parameters
|
||||
foreach (var parameter in Value.Parameters)
|
||||
{
|
||||
if (parameter.Example != null)
|
||||
{
|
||||
exampleData[parameter.Name] = parameter.Example;
|
||||
}
|
||||
else if (parameter.Schema?.Example != null)
|
||||
{
|
||||
exampleData[parameter.Name] = parameter.Schema.Example;
|
||||
}
|
||||
else
|
||||
{
|
||||
exampleData[parameter.Name] = GenerateExampleFromSchema(parameter.Schema);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate example data for request body
|
||||
if (Value.RequestBody?.Content != null)
|
||||
{
|
||||
foreach (var content in Value.RequestBody.Content)
|
||||
{
|
||||
if (content.Value.Example != null)
|
||||
{
|
||||
exampleData[content.Key] = content.Value.Example;
|
||||
}
|
||||
else if (content.Value.Schema?.Example != null)
|
||||
{
|
||||
exampleData[content.Key] = content.Value.Schema.Example;
|
||||
}
|
||||
else
|
||||
{
|
||||
exampleData[content.Key] = GenerateExampleFromSchema(content.Value.Schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JsonSerializer.Serialize(exampleData, new JsonSerializerOptions { WriteIndented = true });
|
||||
}
|
||||
|
||||
private object GenerateExampleFromSchema(OpenApiSchema? schema)
|
||||
{
|
||||
if (schema == null)
|
||||
{
|
||||
return new object();
|
||||
}
|
||||
|
||||
if (schema.Type == "object" && schema.Properties != null)
|
||||
{
|
||||
var obj = new Dictionary<string, object>();
|
||||
foreach (var property in schema.Properties)
|
||||
{
|
||||
obj[property.Key] = GenerateExampleFromSchema(property.Value);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (schema.Type == "array" && schema.Items != null)
|
||||
{
|
||||
return new[] { GenerateExampleFromSchema(schema.Items) };
|
||||
}
|
||||
|
||||
return schema.Default ?? schema.Example ?? GetDefaultValueForType(schema.Type);
|
||||
}
|
||||
|
||||
private object GetDefaultValueForType(string? type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
"string" => "string",
|
||||
"integer" => 0,
|
||||
"number" => 0.0,
|
||||
"boolean" => false,
|
||||
_ => new object()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,6 @@
|
||||
<PackageVersion Include="Microsoft.OpenApi.Readers" Version="1.6.23" />
|
||||
<PackageVersion Include="Swashbuckle.AspNetCore.ReDoc" Version="6.5.0" />
|
||||
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
|
||||
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
|
||||
<PackageVersion Include="System.Text.Json" Version="9.0.3" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Loading…
x
Reference in New Issue
Block a user