Skip to content

Commit 50fec01

Browse files
committed
Adds a method for visiting IBaseDocument instances
1 parent 907e1df commit 50fec01

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ public override void Visit(ref JsonSchema schema)
221221
schema = builder.Build();
222222
}
223223

224+
public override void Visit(IBaseDocument document) { }
225+
224226
private Dictionary<string, JsonSchema> ResolveJsonSchemas(IDictionary<string, JsonSchema> schemas)
225227
{
226228
var resolvedSchemas = new Dictionary<string, JsonSchema>();

src/Microsoft.OpenApi/Services/OpenApiVisitorBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ public virtual void Visit(OpenApiExternalDocs externalDocs)
243243
public virtual void Visit(ref JsonSchema schema)
244244
{
245245
}
246-
246+
247+
public virtual void Visit(IBaseDocument document) { }
248+
247249
/// <summary>
248250
/// Visits <see cref="JsonSchema"/>
249251
/// </summary>

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ internal void Walk(OpenApiEncoding encoding)
808808
internal JsonSchema Walk(JsonSchema schema, bool isComponent = false)
809809
{
810810
if (schema == null
811-
|| (schema.GetRef() != null && !isComponent))
811+
|| ProcessSchemaAsReference(schema, isComponent))
812812
{
813813
return schema;
814814
}
@@ -1162,6 +1162,18 @@ private bool ProcessAsReference(IOpenApiReferenceable referenceable, bool isComp
11621162
}
11631163
return isReference;
11641164
}
1165+
1166+
private bool ProcessSchemaAsReference(IBaseDocument baseDocument, bool isComponent = false)
1167+
{
1168+
var schema = baseDocument as JsonSchema;
1169+
var isReference = schema?.GetRef() != null && !isComponent;
1170+
if (isReference)
1171+
{
1172+
_visitor.Visit(baseDocument);
1173+
}
1174+
1175+
return isReference;
1176+
}
11651177
}
11661178

11671179
/// <summary>

test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,15 @@ public override void Visit(OpenApiMediaType mediaType)
291291
Locations.Add(this.PathString);
292292
}
293293

294+
public override void Visit(IBaseDocument document)
295+
{
296+
var schema = document as JsonSchema;
297+
VisitJsonSchema(schema);
298+
}
299+
294300
public override void Visit(ref JsonSchema schema)
295301
{
296-
if (schema.GetRef() != null)
297-
{
298-
Locations.Add("referenceAt: " + this.PathString);
299-
}
300-
else
301-
{
302-
Locations.Add(this.PathString);
303-
}
302+
VisitJsonSchema(schema);
304303
}
305304

306305
public override void Visit(IList<OpenApiTag> openApiTags)
@@ -317,5 +316,17 @@ public override void Visit(OpenApiServer server)
317316
{
318317
Locations.Add(this.PathString);
319318
}
319+
320+
private void VisitJsonSchema(JsonSchema schema)
321+
{
322+
if (schema.GetRef() != null)
323+
{
324+
Locations.Add("referenceAt: " + this.PathString);
325+
}
326+
else
327+
{
328+
Locations.Add(this.PathString);
329+
}
330+
}
320331
}
321332
}

0 commit comments

Comments
 (0)