Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.OpenApi
{
internal static class OpenAPIWriterExtensions

Check warning on line 3 in src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Rename class 'OpenAPIWriterExtensions' to match pascal case naming rules, consider using 'OpenApiWriterExtensions'.
{
/// <summary>
/// Temporary extension method until we add Settings property to IOpenApiWriter in next major version
Expand All @@ -15,5 +15,13 @@
}
return new();
}

internal static void CountInlineReference(this IOpenApiWriter openApiWriter)
{
if (openApiWriter is OpenApiWriterBase @base)
{
@base.CountInlineReference();
}
}
}
}
9 changes: 7 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public virtual void SerializeAsV3(IOpenApiWriter writer)
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}

internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);
Expand Down Expand Up @@ -175,7 +175,12 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);

// schema
var targetSchema = Schema switch {
if (Schema is OpenApiSchemaReference)
{
writer.CountInlineReference();
}
var targetSchema = Schema switch
{
OpenApiSchemaReference schemaReference => schemaReference.RecursiveTarget,
OpenApiSchema schema => schema,
_ => null,
Expand Down
13 changes: 9 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public bool Deprecated { get; set; }

/// <inheritdoc/>
[Obsolete("Use of AllowEmptyValue is not recommended and it is likely to be removed in a later revision.")]

Check warning on line 36 in src/Microsoft.OpenApi/Models/OpenApiParameter.cs

View workflow job for this annotation

GitHub Actions / Build

Do not forget to remove this deprecated code someday.
public bool AllowEmptyValue { get; set; }

/// <inheritdoc/>
Expand Down Expand Up @@ -113,11 +113,11 @@
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}

internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);

// Validate that Cookie style is only used in OpenAPI 3.2 and later
if (Style == ParameterStyle.Cookie && version < OpenApiSpecVersion.OpenApi3_2)
{
Expand Down Expand Up @@ -201,7 +201,7 @@
/// </summary>
/// <param name="writer">Writer to use for the serialization</param>
/// <param name="extensionsClone">Extensions clone</param>
internal virtual void WriteRequestBodySchemaForV2(IOpenApiWriter writer, Dictionary<string, IOpenApiExtension>? extensionsClone)

Check warning on line 204 in src/Microsoft.OpenApi/Models/OpenApiParameter.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed.
{
// In V2 parameter's type can't be a reference to a custom object schema or can't be of type object
// So in that case map the type as string.
Expand All @@ -228,7 +228,12 @@
// uniqueItems
// enum
// multipleOf
var targetSchema = Schema switch {
if (Schema is OpenApiSchemaReference)
{
writer.CountInlineReference();
}
var targetSchema = Schema switch
{
OpenApiSchemaReference schemaReference => schemaReference.RecursiveTarget,
OpenApiSchema schema => schema,
_ => null,
Expand All @@ -253,7 +258,7 @@

if (this.In == ParameterLocation.Query && Schema?.Type == JsonSchemaType.Array)
{
if (this.Style == ParameterStyle.Form && this.Explode == true)

Check warning on line 261 in src/Microsoft.OpenApi/Models/OpenApiParameter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove the unnecessary Boolean literal(s).
{
writer.WriteProperty("collectionFormat", "multi");
}
Expand All @@ -273,7 +278,7 @@
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);

// Validate that Cookie style is only used in OpenAPI 3.2 and later
if (Style == ParameterStyle.Cookie)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// <typeparam name="T">The concrete class implementation type for the model.</typeparam>
/// <typeparam name="U">The interface type for the model.</typeparam>
/// <typeparam name="V">The type for the reference holding the additional fields and annotations</typeparam>
public abstract class BaseOpenApiReferenceHolder<T, U, V> : IOpenApiReferenceHolder<T, U, V> where T : class, IOpenApiReferenceable, U where U : IOpenApiReferenceable, IOpenApiSerializable where V : BaseOpenApiReference, new()

Check warning on line 12 in src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs

View workflow job for this annotation

GitHub Actions / Build

Reduce the number of generic parameters in the 'BaseOpenApiReferenceHolder' class to no more than the 2 authorized.
{
[ThreadStatic]
private static HashSet<BaseOpenApiReferenceHolder<T, U, V>>? t_activeReferenceAccesses;
Expand Down Expand Up @@ -70,7 +70,7 @@
return ExecuteWithReferenceGuard(ref t_activeReferenceAccesses, holder, action);
}

private static TResult ExecuteWithTargetActionGuard<TResult>(

Check warning on line 73 in src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs

View workflow job for this annotation

GitHub Actions / Build

Change return type to 'void'; not a single caller uses the returned value.
BaseOpenApiReferenceHolder<T, U, V> holder,
Func<TResult> action)
{
Expand Down Expand Up @@ -137,7 +137,7 @@

private sealed class ReferenceHolderComparer : IEqualityComparer<BaseOpenApiReferenceHolder<T, U, V>>
{
internal static ReferenceHolderComparer Instance { get; } = new();

Check warning on line 140 in src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs

View workflow job for this annotation

GitHub Actions / Build

A static field in a generic type is not shared among instances of different close constructed types.

public bool Equals(BaseOpenApiReferenceHolder<T, U, V>? x, BaseOpenApiReferenceHolder<T, U, V>? y)
{
Expand All @@ -163,7 +163,7 @@
protected BaseOpenApiReferenceHolder(BaseOpenApiReferenceHolder<T, U, V> source)
{
Utils.CheckArgumentNull(source);
Reference = CopyReference(source.Reference);

Check warning on line 166 in src/Microsoft.OpenApi/Models/References/BaseOpenApiReferenceHolder.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this call from a constructor to the overridable 'CopyReference' method.
//no need to copy summary and description as if they are not overridden, they will be fetched from the target
//if they are, the reference copy will handle it
}
Expand Down Expand Up @@ -207,7 +207,7 @@
/// <inheritdoc/>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(Reference)
if (!writer.GetSettings().ShouldInlineReference(Reference)
|| Reference.Type == ReferenceType.Tag) // tags are held as references need to drop in.
{
Reference.SerializeAsV3(writer);
Expand Down Expand Up @@ -267,6 +267,7 @@
Action<IOpenApiWriter, U> action)
{
Utils.CheckArgumentNull(writer);
writer.CountInlineReference();
ApplyToTarget(element => action(writer, element));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ protected override BaseOpenApiReference CopyReference(BaseOpenApiReference sourc
/// <inheritdoc/>
public override void SerializeAsV31(IOpenApiWriter writer)
{
writer.CountInlineReference();
CopyReferenceAsTargetElementWithOverrides(new OpenApiMediaType()).SerializeAsV31(writer);
}
/// <inheritdoc/>
public override void SerializeAsV3(IOpenApiWriter writer)
{
writer.CountInlineReference();
CopyReferenceAsTargetElementWithOverrides(new OpenApiMediaType()).SerializeAsV3(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public override void SerializeAsV2(IOpenApiWriter writer)
{
if (writer.GetSettings().ShouldInlineReference(Reference))
{
writer.CountInlineReference();
return GetFromTarget(target => target.ConvertToBodyParameter(writer));
}

Expand All @@ -74,6 +75,7 @@ public override void SerializeAsV2(IOpenApiWriter writer)
{
if (writer.GetSettings().ShouldInlineReference(Reference))
{
writer.CountInlineReference();
return GetFromTarget(target => target.ConvertToFormDataParameters(writer));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
/// <inheritdoc/>
public OpenApiDiscriminator? Discriminator { get => Reference.Discriminator ?? GetFromTarget(static target => target.Discriminator); set => Reference.Discriminator = value; }
/// <inheritdoc/>
[Obsolete("Use Examples instead.")]

Check warning on line 170 in src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs

View workflow job for this annotation

GitHub Actions / Build

Do not forget to remove this deprecated code someday.
public JsonNode? Example { get => Reference.Example ?? GetFromTarget(static target => target.Example); set => Reference.Example = value; }
/// <inheritdoc/>
public IList<JsonNode>? Examples
Expand Down Expand Up @@ -256,8 +256,14 @@
}
else
{
SerializeInternal(writer, (w, element) => action(w, element));
writer.GetSettings().LoopDetector.PopLoop<IOpenApiSchema>();
try
{
SerializeInternal(writer, (w, element) => action(w, element));
}
finally
{
writer.GetSettings().LoopDetector.PopLoop<IOpenApiSchema>();
}
}

}
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.OpenApi/Properties/SRResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<data name="IndentationLevelInvalid" xml:space="preserve">
<value>Indentation level cannot be lower than 0.</value>
</data>
<data name="MaximumInlineReferenceCountExceeded" xml:space="preserve">
<value>The maximum number of inline references ({0}) has been exceeded.</value>
</data>
<data name="InputItemShouldBeType" xml:space="preserve">
<value>The input item should be in type of '{0}'.</value>
</data>
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.OpenApi/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#nullable enable
Microsoft.OpenApi.OpenApiDocument.GetOperationById(string! operationId) -> Microsoft.OpenApi.OpenApiOperation?
Microsoft.OpenApi.OpenApiWriterSettings.MaxInlineReferenceCount.get -> int
Microsoft.OpenApi.OpenApiWriterSettings.MaxInlineReferenceCount.set -> void
17 changes: 16 additions & 1 deletion src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
/// Number which specifies the level of indentation.
/// </summary>
private int _indentLevel;
private int _inlineReferenceCount;

/// <summary>
/// Initializes a new instance of the <see cref="OpenApiWriterBase"/> class.
Expand Down Expand Up @@ -70,6 +71,20 @@
/// </summary>
protected TextWriter Writer { get; }

internal void CountInlineReference()
{
if (_inlineReferenceCount >= Settings.MaxInlineReferenceCount)
{
throw new OpenApiWriterException(
string.Format(
CultureInfo.InvariantCulture,
SRResource.MaximumInlineReferenceCountExceeded,
Settings.MaxInlineReferenceCount));
}

_inlineReferenceCount++;
}

/// <summary>
/// Write start object.
/// </summary>
Expand Down Expand Up @@ -130,7 +145,7 @@
/// Write float value.
/// </summary>
/// <param name="value">The float value.</param>
public virtual void WriteValue(float value)

Check warning on line 148 in src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

View workflow job for this annotation

GitHub Actions / Build

All 'WriteValue' method overloads should be adjacent.
{
WriteValueSeparator();
Writer.Write(value);
Expand Down Expand Up @@ -319,7 +334,7 @@
Writer.Write(IndentationString);
}
}

/// <summary>
/// Get current scope.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public class OpenApiWriterSettings
{
private const int DefaultMaxInlineReferenceCount = 10_000;

internal LoopDetector LoopDetector { get; } = new();

/// <summary>
/// Indicates if local references should be rendered as an inline object
/// </summary>
Expand All @@ -16,6 +19,12 @@ public class OpenApiWriterSettings
/// </summary>
public bool InlineExternalReferences { get; set; }

/// <summary>
/// Gets or sets the maximum number of references that can be expanded inline by a writer.
/// </summary>
/// <remarks>The default is 10,000.</remarks>
public int MaxInlineReferenceCount { get; set; } = DefaultMaxInlineReferenceCount;

internal bool ShouldInlineReference(BaseOpenApiReference reference)
{
return (reference.IsLocal && InlineLocalReferences)
Expand Down
Loading
Loading