mirror of
https://github.com/unclshura/BlazorOpenApi.git
synced 2025-12-21 09:51:53 +00:00
193 lines
6.3 KiB
Plaintext
193 lines
6.3 KiB
Plaintext
<TocMember Title="@TocTitle" Anchor="@_anchor">
|
|
@if (Value != null)
|
|
{
|
|
<div id="@_anchor" class="op-header @OperationClass" @onclick="Expand">
|
|
<div class="op-type">@Operation</div>
|
|
<div class="spacer op-header-text">-</div>
|
|
<div class="op-header-text" id="@Value.OperationId">@Endpoint</div>
|
|
<div class="spacer op-header-text">-</div>
|
|
<div class="op-summary op-header-text">@Value.Summary</div>
|
|
@if (Value.Tags.Count > 0)
|
|
{
|
|
<div class="op-tags">
|
|
@foreach (var tag in Value.Tags)
|
|
{
|
|
<Tooltip class="op-tag" TooltipText="@tag.Description">
|
|
<Text Class="op-tag-name" Value="@tag.Name" />
|
|
</Tooltip>
|
|
}
|
|
</div>
|
|
}
|
|
@if (!Collapsed)
|
|
{
|
|
<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
<path d="M18 15L12 9L6 15" stroke="var(--oa-fg-lighter)" stroke-width="2" />
|
|
</svg>
|
|
}
|
|
else
|
|
{
|
|
<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
<path d="M18 9L12 15L6 9" stroke="var(--oa-fg-lighter)" stroke-width="2" />
|
|
</svg>
|
|
}
|
|
|
|
</div>
|
|
@if (!Collapsed)
|
|
{
|
|
<div class="operation">
|
|
|
|
@if (Value != null)
|
|
{
|
|
<MarkdownControl Value="@Value.Description" />
|
|
|
|
if (Value.Servers?.Count > 0)
|
|
{
|
|
<ServersControl Value="@Value.Servers" />
|
|
}
|
|
|
|
<ParametersControl Value="@Value.Parameters" />
|
|
<RequestBodyControl Value="@Value.RequestBody" />
|
|
<ResponsesControl Value="@Value.Responses" />
|
|
|
|
@if (true)
|
|
{
|
|
var example = GenerateExampleData();
|
|
if (!string.IsNullOrWhiteSpace(example))
|
|
{
|
|
<div class="example">
|
|
<h3 class="e-title">Example Data</h3>
|
|
<textarea type="text" readonly multiple rows="20" class="e-item">@GenerateExampleData()</textarea>
|
|
</div>
|
|
}
|
|
|
|
}
|
|
}
|
|
</div>
|
|
}
|
|
}
|
|
</TocMember>
|
|
|
|
@code {
|
|
[Parameter]
|
|
public OperationType Operation { get; set; }
|
|
[Parameter]
|
|
public OpenApiOperation? Value { get; set; }
|
|
[Parameter]
|
|
public string Endpoint { get; set; } = "";
|
|
|
|
[CascadingParameter(Name = "OpenAPIUI_TOC")] public ITableOfContentsTree Tree { get; set; } = null!;
|
|
[CascadingParameter(Name = "OpenAPIUI_TOC_Parent")] public string Parent { get; set; } = "";
|
|
|
|
private string _anchor = $"op_anc{Random.Shared.Next():X8}";
|
|
private string TocTitle => $"{Endpoint} - [{Operation.ToString().ToUpper()}] {Value?.Summary}";
|
|
|
|
private string OperationClass => $"op-{Operation.ToString().ToLower()}";
|
|
|
|
private bool Collapsed => Tree.IsCollapsed(_anchor);
|
|
|
|
private void Expand()
|
|
{
|
|
var collapsed = !Tree.IsCollapsed(_anchor);
|
|
Tree.Collapse(_anchor, collapsed);
|
|
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.Where(x => x.In == ParameterLocation.Query))
|
|
{
|
|
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
|
|
{
|
|
var ex = GenerateExampleFromSchema(content.Value.Schema);
|
|
if ( ex != null )
|
|
exampleData[content.Key] = ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
return exampleData.Count == 0
|
|
? ""
|
|
: JsonSerializer.Serialize(exampleData, new JsonSerializerOptions { WriteIndented = true })
|
|
;
|
|
}
|
|
|
|
private object? GenerateExampleFromSchema(OpenApiSchema? schema)
|
|
{
|
|
if (schema == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (schema.Type == "object" && schema.Properties != null)
|
|
{
|
|
var obj = new Dictionary<string, object?>();
|
|
foreach (var property in schema.Properties)
|
|
{
|
|
var ex = GenerateExampleFromSchema(property.Value);
|
|
if (ex != null)
|
|
obj[property.Key] = ex;
|
|
}
|
|
if ( obj.Count == 0 )
|
|
return null;
|
|
return obj;
|
|
}
|
|
|
|
if (schema.Type == "array" && schema.Items != null)
|
|
{
|
|
var ex = GenerateExampleFromSchema(schema.Items);
|
|
if (ex == null)
|
|
return null;
|
|
return new[] { ex };
|
|
}
|
|
|
|
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,
|
|
_ => null
|
|
};
|
|
}
|
|
}
|