Skip to content

Commit ae52035

Browse files
committed
Refactor code to resolve Oneof and AnyOf schemas
1 parent 9198a2c commit ae52035

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ static PowerShellFormatter()
4444
// 6. Add AdditionalProperties to object schemas.
4545

4646
public override void Visit(ref JsonSchema schema)
47-
{
48-
AddAdditionalPropertiesToSchema(schema);
47+
{
48+
AddAdditionalPropertiesToSchema(ref schema);
4949
schema = ResolveAnyOfSchema(ref schema);
5050
schema = ResolveOneOfSchema(ref schema);
5151

@@ -174,7 +174,7 @@ private static IList<OpenApiParameter> ResolveFunctionParameters(IList<OpenApiPa
174174
return parameters;
175175
}
176176

177-
private void AddAdditionalPropertiesToSchema(JsonSchema schema)
177+
private void AddAdditionalPropertiesToSchema(ref JsonSchema schema)
178178
{
179179
if (schema != null && !_schemaLoop.Contains(schema) && schema.GetJsonType().Equals(SchemaValueType.Object))
180180
{
@@ -200,6 +200,7 @@ private void AddAdditionalPropertiesToSchema(JsonSchema schema)
200200
_schemaLoop.Push(additionalProps);
201201
}
202202
}
203+
203204
}
204205

205206
private static JsonSchema ResolveOneOfSchema(ref JsonSchema schema)
@@ -254,6 +255,14 @@ private static JsonSchema FlattenSchema(JsonSchema schema, JsonSchema newSchema)
254255
private static JsonSchema CopySchema(JsonSchema schema, JsonSchema newSchema)
255256
{
256257
var schemaBuilder = new JsonSchemaBuilder();
258+
var keywords = schema.Keywords;
259+
if (keywords != null)
260+
{
261+
foreach (var keyword in keywords)
262+
{
263+
schemaBuilder.Add(keyword);
264+
}
265+
}
257266

258267
if (schema.GetTitle() == null && newSchema.GetTitle() is { } title)
259268
{

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal void Walk(OpenApiComponents components)
127127
{
128128
foreach (var item in components.Schemas)
129129
{
130-
Walk(item.Key, () => Walk(item.Value, isComponent: true));
130+
Walk(item.Key, () => components.Schemas[item.Key] = Walk(item.Value, isComponent: true));
131131
}
132132
}
133133
});
@@ -498,8 +498,7 @@ internal void Walk(OpenApiPathItem pathItem, bool isComponent = false)
498498

499499
_visitor.Visit(pathItem);
500500

501-
// The path may be a reference
502-
if (pathItem != null && !ProcessAsReference(pathItem))
501+
if (pathItem != null)
503502
{
504503
Walk(OpenApiConstants.Parameters, () => Walk(pathItem.Parameters));
505504
Walk(pathItem.Operations);
@@ -850,9 +849,20 @@ internal JsonSchema Walk(JsonSchema schema, bool isComponent = false)
850849
{
851850
Walk("properties", () =>
852851
{
852+
var props = new Dictionary<string, JsonSchema>();
853+
var builder = new JsonSchemaBuilder();
854+
foreach(var keyword in schema.Keywords)
855+
{
856+
builder.Add(keyword);
857+
}
858+
853859
foreach (var item in schema.GetProperties())
854860
{
855-
Walk(item.Key, () => Walk(item.Value));
861+
var key = item.Key;
862+
JsonSchema newSchema = null;
863+
Walk(key, () => newSchema = Walk(item.Value));
864+
props.Add(key, newSchema);
865+
schema = builder.Properties(props);
856866
}
857867
});
858868
}

0 commit comments

Comments
 (0)