Skip to content

Added Example property to OpenApi attribute #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public class OpenApiPropertyAttribute : Attribute
/// Gets or sets the deprecated flag.
/// </summary>
public virtual bool Deprecated { get; set; }

/// <summary>
/// Gets or sets the example value of the property.
/// </summary>
public virtual object Example { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
schema.Default = this.GetOpenApiPropertyDefault(attr as OpenApiPropertyAttribute);
schema.Description = this.GetOpenApiPropertyDescription(attr as OpenApiPropertyAttribute);
schema.Deprecated = this.GetOpenApiPropertyDeprecated(attr as OpenApiPropertyAttribute);
schema.Example = this.GetOpenApiPropertyExample(attr as OpenApiPropertyAttribute);
}

attr = attributes.OfType<OpenApiSchemaVisibilityAttribute>().SingleOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ protected string Visit(IAcceptor acceptor, string name, string title, string dat
schema.Nullable = this.GetOpenApiPropertyNullable(attr as OpenApiPropertyAttribute);
schema.Description = this.GetOpenApiPropertyDescription(attr as OpenApiPropertyAttribute);
schema.Deprecated = this.GetOpenApiPropertyDeprecated(attr as OpenApiPropertyAttribute);
schema.Example = this.GetOpenApiPropertyExample(attr as OpenApiPropertyAttribute);
}

attr = attributes.OfType<OpenApiSchemaVisibilityAttribute>().SingleOrDefault();
Expand Down Expand Up @@ -392,6 +393,97 @@ protected bool GetOpenApiPropertyDeprecated(OpenApiPropertyAttribute attr)
return attr.Deprecated;
}

/// <summary>
/// Gets the property example.
/// </summary>
/// <param name="attr"><see cref="OpenApiPropertyAttribute"/> instance.</param>
/// <returns>Returns the property example.</returns>
protected IOpenApiAny GetOpenApiPropertyExample(OpenApiPropertyAttribute attr)
{
var @example = attr.Example;
if (@example.IsNullOrDefault())
{
return null;
}

if (@example is bool)
{
return new OpenApiBoolean((bool)@example);
}

if (@example is DateTime)
{
return new OpenApiDateTime((DateTime)@example);
}

if (@example is TimeSpan)
{
return new OpenApiString(@example.ToString());
}

if (@example is DateTimeOffset)
{
return new OpenApiDateTime((DateTimeOffset)@example);
}

if (@example is float)
{
return new OpenApiFloat((float)@example);
}

if (@example is double)
{
return new OpenApiDouble((double)@example);
}

if (@example is decimal)
{
return new OpenApiDouble(Convert.ToDouble(@example));
}

if (@example is byte[])
{
return new OpenApiByte((byte[])@example);
}

if (@example is short)
{
return new OpenApiInteger((short)@example);
}

if (@example is int)
{
return new OpenApiInteger((int)@example);
}

if (@example is long)
{
return new OpenApiLong((long)@example);
}

if (@example is ushort)
{
return new OpenApiInteger(Convert.ToInt16(@example));
}

if (@example is uint)
{
return new OpenApiInteger(Convert.ToInt32(@example));
}

if (@example is ulong)
{
return new OpenApiLong(Convert.ToInt64(@example));
}

if (@example is Guid)
{
return new OpenApiString(Convert.ToString(@example));
}

return new OpenApiString((string)@example);
}

/// <summary>
/// Gets whether Newtonsoft Serialization allows nullability based on the JsonPropertyAttribute
/// </summary>
Expand Down