Skip to content

Commit

Permalink
Added overridden version of the AsyncAPiSeriableExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ByronMayne committed Mar 28, 2024
1 parent dd86a57 commit aae7aa2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
36 changes: 33 additions & 3 deletions src/LEGO.AsyncAPI/Models/AsyncApiSerializableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ public static class AsyncApiSerializableExtensions
/// <param name="element">The AsyncApi element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specificationVersion">The AsyncApi specification version.</param>
[Obsolete($"Please use overridden version that accepts a {nameof(AsyncApiWriterSettings)} instance.")]
public static void SerializeAsJson<T>(this T element, Stream stream, AsyncApiVersion specificationVersion)
where T : IAsyncApiSerializable
{
element.Serialize(stream, specificationVersion, AsyncApiFormat.Json);
element.SerializeAsJson(stream, specificationVersion, AsyncApiWriterSettings.Default);
}

/// <summary>
/// Serialize the <see cref="IAsyncApiSerializable"/> to the AsyncApi document (JSON) using the given stream and specification version.
/// </summary>
/// <typeparam name="T">the <see cref="IAsyncApiSerializable"/>.</typeparam>
/// <param name="element">The AsyncApi element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specificationVersion">The AsyncApi specification version.</param>
/// <param name="settings">The settings used for writing</param>
public static void SerializeAsJson<T>(this T element, Stream stream, AsyncApiVersion specificationVersion, AsyncApiWriterSettings settings)
where T : IAsyncApiSerializable
{
element.Serialize(stream, specificationVersion, AsyncApiFormat.Json, settings);
}

/// <summary>
Expand All @@ -32,10 +47,25 @@ public static void SerializeAsJson<T>(this T element, Stream stream, AsyncApiVer
/// <param name="element">The AsyncApi element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specificationVersion">The AsyncApi specification version.</param>
[Obsolete($"Please use overridden version that accepts a {nameof(AsyncApiWriterSettings)} instance.")]
public static void SerializeAsYaml<T>(this T element, Stream stream, AsyncApiVersion specificationVersion)
where T : IAsyncApiSerializable
{
element.Serialize(stream, specificationVersion, AsyncApiFormat.Yaml);
element.SerializeAsYaml(stream, specificationVersion, AsyncApiWriterSettings.Default);
}

/// <summary>
/// Serializes the <see cref="IAsyncApiSerializable"/> to the AsyncApi document (YAML) using the given stream and specification version.
/// </summary>
/// <typeparam name="T">the <see cref="IAsyncApiSerializable"/>.</typeparam>
/// <param name="element">The AsyncApi element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specificationVersion">The AsyncApi specification version.</param>
/// <param name="settings">The settings used for writing</param>
public static void SerializeAsYaml<T>(this T element, Stream stream, AsyncApiVersion specificationVersion, AsyncApiWriterSettings settings)
where T : IAsyncApiSerializable
{
element.Serialize(stream, specificationVersion, AsyncApiFormat.Yaml, settings);
}

/// <summary>
Expand All @@ -45,7 +75,7 @@ public static void SerializeAsYaml<T>(this T element, Stream stream, AsyncApiVer
/// <typeparam name="T">the <see cref="IAsyncApiSerializable"/>.</typeparam>
/// <param name="element">The AsyncApi element.</param>
/// <param name="stream">The given stream.</param>
/// <param name="specVersion">The AsyncApi specification version.</param>
/// <param name="specificationVersion">The AsyncApi specification version.</param>
/// <param name="format">The output format (JSON or YAML).</param>
[Obsolete($"Please use overridden version that accepts a {nameof(AsyncApiWriterSettings)} instance.")]
public static void Serialize<T>(
Expand Down
34 changes: 32 additions & 2 deletions src/LEGO.AsyncAPI/Writers/AsyncApiWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@ namespace LEGO.AsyncAPI.Writers
{
using LEGO.AsyncAPI.Models;

/// <summary>
/// Contains settings for writing async api.
/// </summary>
public class AsyncApiWriterSettings : AsyncApiSettings
{
private ReferenceInlineSetting referenceInline = ReferenceInlineSetting.DoNotInlineReferences;

internal LoopDetector LoopDetector { get; } = new LoopDetector();
static AsyncApiWriterSettings()
{
Default = new AsyncApiWriterSettings();
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiWriterSettings"/> class.
/// </summary>
public AsyncApiWriterSettings()
{
this.InlineReferences = false;
this.LoopDetector = new LoopDetector();
}

/// <summary>
/// Gets the default settings to use for writing async api.
/// </summary>
public static AsyncApiWriterSettings Default { get; }

/// <summary>
/// Gets or sets indicates how references in the source document should be handled.
Expand Down Expand Up @@ -38,8 +58,18 @@ public ReferenceInlineSetting ReferenceInline
/// <summary>
/// Gets or sets a value indicating whether indicates if local references should be rendered as an inline object.
/// </summary>
public bool InlineReferences { get; set; } = false;
public bool InlineReferences { get; set; }

/// <summary>
/// Figures out if a loop exists.
/// </summary>
internal LoopDetector LoopDetector { get; }

/// <summary>
/// Returns back if the refernece should be inlined or not.
/// </summary>
/// <param name="reference">The refernece.</param>
/// <returns>True if it should be inlined otherwise false.</returns>
public bool ShouldInlineReference(AsyncApiReference reference)
{
return this.InlineReferences;
Expand Down

0 comments on commit aae7aa2

Please sign in to comment.