Skip to content
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

fix: open api schema reference proxy design pattern implementation #2106

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 18 additions & 28 deletions src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class PowerShellFormatter : OpenApiVisitorBase
{
private const string DefaultPutPrefix = ".Update";
private const string PowerShellPutPrefix = ".Set";
private readonly Stack<OpenApiSchema> _schemaLoop = new();
private readonly Stack<IOpenApiSchema> _schemaLoop = new();
private static readonly Regex s_oDataCastRegex = new("(.*(?<=[a-z]))\\.(As(?=[A-Z]).*)", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex s_hashSuffixRegex = new(@"^[^-]+", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex s_oDataRefRegex = new("(?<=[a-z])Ref(?=[A-Z])", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
Expand All @@ -42,7 +42,7 @@ static PowerShellFormatter()
// 5. Fix anyOf and oneOf schema.
// 6. Add AdditionalProperties to object schemas.

public override void Visit(OpenApiSchema schema)
public override void Visit(IOpenApiSchema schema)
{
AddAdditionalPropertiesToSchema(schema);
ResolveAnyOfSchema(schema);
Expand Down Expand Up @@ -165,22 +165,22 @@ private static void ResolveFunctionParameters(IList<IOpenApiParameter> parameter
// Replace content with a schema object of type array
// for structured or collection-valued function parameters
parameter.Content = null;
parameter.Schema = new()
parameter.Schema = new OpenApiSchema()
{
Type = JsonSchemaType.Array,
Items = new()
Items = new OpenApiSchema()
{
Type = JsonSchemaType.String
}
};
}
}

private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
private void AddAdditionalPropertiesToSchema(IOpenApiSchema schema)
{
if (schema != null && !_schemaLoop.Contains(schema) && schema.Type.Equals(JsonSchemaType.Object))
if (schema is OpenApiSchema openApiSchema && !_schemaLoop.Contains(schema) && schema.Type.Equals(JsonSchemaType.Object))
{
schema.AdditionalProperties = new() { Type = JsonSchemaType.Object };
openApiSchema.AdditionalProperties = new OpenApiSchema() { Type = JsonSchemaType.Object };

/* Because 'additionalProperties' are now being walked,
* we need a way to keep track of visited schemas to avoid
Expand All @@ -190,39 +190,29 @@ private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
}
}

private static void ResolveOneOfSchema(OpenApiSchema schema)
private static void ResolveOneOfSchema(IOpenApiSchema schema)
{
if (schema.OneOf?.FirstOrDefault() is { } newSchema)
if (schema is OpenApiSchema openApiSchema && schema.OneOf?.FirstOrDefault() is OpenApiSchema newSchema)
{
schema.OneOf = null;
FlattenSchema(schema, newSchema);
openApiSchema.OneOf = null;
FlattenSchema(openApiSchema, newSchema);
}
}

private static void ResolveAnyOfSchema(OpenApiSchema schema)
private static void ResolveAnyOfSchema(IOpenApiSchema schema)
{
if (schema.AnyOf?.FirstOrDefault() is { } newSchema)
if (schema is OpenApiSchema openApiSchema && schema.AnyOf?.FirstOrDefault() is OpenApiSchema newSchema)
{
schema.AnyOf = null;
FlattenSchema(schema, newSchema);
openApiSchema.AnyOf = null;
FlattenSchema(openApiSchema, newSchema);
}
}

private static void FlattenSchema(OpenApiSchema schema, OpenApiSchema newSchema)
{
if (newSchema != null)
{
if (newSchema.Reference != null)
{
schema.Reference = newSchema.Reference;
schema.UnresolvedReference = true;
}
else
{
// Copies schema properties based on https://github.com/microsoft/OpenAPI.NET.OData/pull/264.
CopySchema(schema, newSchema);
}
}
if (newSchema is null) return;
// Copies schema properties based on https://github.com/microsoft/OpenAPI.NET.OData/pull/264.
CopySchema(schema, newSchema);
}

private static void CopySchema(OpenApiSchema schema, OpenApiSchema newSchema)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/StatsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Visit(IOpenApiParameter parameter)

public int SchemaCount { get; set; }

public override void Visit(OpenApiSchema schema)
public override void Visit(IOpenApiSchema schema)
{
SchemaCount++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Workbench/StatsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Visit(IOpenApiParameter parameter)

public int SchemaCount { get; set; }

public override void Visit(OpenApiSchema schema)
public override void Visit(IOpenApiSchema schema)
{
SchemaCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface IOpenApiHeader : IOpenApiDescribedElement, IOpenApiSerializable
/// <summary>
/// The schema defining the type used for the request body.
/// </summary>
public OpenApiSchema Schema { get; }
public IOpenApiSchema Schema { get; }

/// <summary>
/// Example of the media type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public interface IOpenApiParameter : IOpenApiDescribedElement, IOpenApiSerializa
/// <summary>
/// The schema defining the type used for the parameter.
/// </summary>
public OpenApiSchema Schema { get; }
public IOpenApiSchema Schema { get; }

/// <summary>
/// Examples of the media type. Each example SHOULD contain a value
Expand Down
Loading
Loading