Skip to content

Commit

Permalink
Merge pull request #2106 from microsoft/fix/schema-reference
Browse files Browse the repository at this point in the history
fix: open api schema reference proxy design pattern implementation
  • Loading branch information
baywet authored Jan 31, 2025
2 parents 8103c20 + 21253f6 commit e57d049
Show file tree
Hide file tree
Showing 72 changed files with 1,370 additions and 1,380 deletions.
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
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/Interfaces/IOpenApiHeader.cs
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

0 comments on commit e57d049

Please sign in to comment.