Skip to content

Commit e57d049

Browse files
authored
Merge pull request #2106 from microsoft/fix/schema-reference
fix: open api schema reference proxy design pattern implementation
2 parents 8103c20 + 21253f6 commit e57d049

File tree

72 files changed

+1370
-1380
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1370
-1380
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

+18-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class PowerShellFormatter : OpenApiVisitorBase
1616
{
1717
private const string DefaultPutPrefix = ".Update";
1818
private const string PowerShellPutPrefix = ".Set";
19-
private readonly Stack<OpenApiSchema> _schemaLoop = new();
19+
private readonly Stack<IOpenApiSchema> _schemaLoop = new();
2020
private static readonly Regex s_oDataCastRegex = new("(.*(?<=[a-z]))\\.(As(?=[A-Z]).*)", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
2121
private static readonly Regex s_hashSuffixRegex = new(@"^[^-]+", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
2222
private static readonly Regex s_oDataRefRegex = new("(?<=[a-z])Ref(?=[A-Z])", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
@@ -42,7 +42,7 @@ static PowerShellFormatter()
4242
// 5. Fix anyOf and oneOf schema.
4343
// 6. Add AdditionalProperties to object schemas.
4444

45-
public override void Visit(OpenApiSchema schema)
45+
public override void Visit(IOpenApiSchema schema)
4646
{
4747
AddAdditionalPropertiesToSchema(schema);
4848
ResolveAnyOfSchema(schema);
@@ -165,22 +165,22 @@ private static void ResolveFunctionParameters(IList<IOpenApiParameter> parameter
165165
// Replace content with a schema object of type array
166166
// for structured or collection-valued function parameters
167167
parameter.Content = null;
168-
parameter.Schema = new()
168+
parameter.Schema = new OpenApiSchema()
169169
{
170170
Type = JsonSchemaType.Array,
171-
Items = new()
171+
Items = new OpenApiSchema()
172172
{
173173
Type = JsonSchemaType.String
174174
}
175175
};
176176
}
177177
}
178178

179-
private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
179+
private void AddAdditionalPropertiesToSchema(IOpenApiSchema schema)
180180
{
181-
if (schema != null && !_schemaLoop.Contains(schema) && schema.Type.Equals(JsonSchemaType.Object))
181+
if (schema is OpenApiSchema openApiSchema && !_schemaLoop.Contains(schema) && schema.Type.Equals(JsonSchemaType.Object))
182182
{
183-
schema.AdditionalProperties = new() { Type = JsonSchemaType.Object };
183+
openApiSchema.AdditionalProperties = new OpenApiSchema() { Type = JsonSchemaType.Object };
184184

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

193-
private static void ResolveOneOfSchema(OpenApiSchema schema)
193+
private static void ResolveOneOfSchema(IOpenApiSchema schema)
194194
{
195-
if (schema.OneOf?.FirstOrDefault() is { } newSchema)
195+
if (schema is OpenApiSchema openApiSchema && schema.OneOf?.FirstOrDefault() is OpenApiSchema newSchema)
196196
{
197-
schema.OneOf = null;
198-
FlattenSchema(schema, newSchema);
197+
openApiSchema.OneOf = null;
198+
FlattenSchema(openApiSchema, newSchema);
199199
}
200200
}
201201

202-
private static void ResolveAnyOfSchema(OpenApiSchema schema)
202+
private static void ResolveAnyOfSchema(IOpenApiSchema schema)
203203
{
204-
if (schema.AnyOf?.FirstOrDefault() is { } newSchema)
204+
if (schema is OpenApiSchema openApiSchema && schema.AnyOf?.FirstOrDefault() is OpenApiSchema newSchema)
205205
{
206-
schema.AnyOf = null;
207-
FlattenSchema(schema, newSchema);
206+
openApiSchema.AnyOf = null;
207+
FlattenSchema(openApiSchema, newSchema);
208208
}
209209
}
210210

211211
private static void FlattenSchema(OpenApiSchema schema, OpenApiSchema newSchema)
212212
{
213-
if (newSchema != null)
214-
{
215-
if (newSchema.Reference != null)
216-
{
217-
schema.Reference = newSchema.Reference;
218-
schema.UnresolvedReference = true;
219-
}
220-
else
221-
{
222-
// Copies schema properties based on https://github.com/microsoft/OpenAPI.NET.OData/pull/264.
223-
CopySchema(schema, newSchema);
224-
}
225-
}
213+
if (newSchema is null) return;
214+
// Copies schema properties based on https://github.com/microsoft/OpenAPI.NET.OData/pull/264.
215+
CopySchema(schema, newSchema);
226216
}
227217

228218
private static void CopySchema(OpenApiSchema schema, OpenApiSchema newSchema)

src/Microsoft.OpenApi.Hidi/StatsVisitor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override void Visit(IOpenApiParameter parameter)
2020

2121
public int SchemaCount { get; set; }
2222

23-
public override void Visit(OpenApiSchema schema)
23+
public override void Visit(IOpenApiSchema schema)
2424
{
2525
SchemaCount++;
2626
}

src/Microsoft.OpenApi.Workbench/StatsVisitor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override void Visit(IOpenApiParameter parameter)
2020

2121
public int SchemaCount { get; set; }
2222

23-
public override void Visit(OpenApiSchema schema)
23+
public override void Visit(IOpenApiSchema schema)
2424
{
2525
SchemaCount++;
2626
}

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiHeader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface IOpenApiHeader : IOpenApiDescribedElement, IOpenApiSerializable
4545
/// <summary>
4646
/// The schema defining the type used for the request body.
4747
/// </summary>
48-
public OpenApiSchema Schema { get; }
48+
public IOpenApiSchema Schema { get; }
4949

5050
/// <summary>
5151
/// Example of the media type.

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiParameter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public interface IOpenApiParameter : IOpenApiDescribedElement, IOpenApiSerializa
7272
/// <summary>
7373
/// The schema defining the type used for the parameter.
7474
/// </summary>
75-
public OpenApiSchema Schema { get; }
75+
public IOpenApiSchema Schema { get; }
7676

7777
/// <summary>
7878
/// Examples of the media type. Each example SHOULD contain a value

0 commit comments

Comments
 (0)