Skip to content

Commit 3dbff85

Browse files
committed
fix: walk schemas recursively to avoid broken references
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 10319ab commit 3dbff85

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/Microsoft.OpenApi/Services/SchemaReferencesCollectorVisitor.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,40 @@ internal class SchemaReferencesCollectorVisitor : OpenApiVisitorBase
88
public override void Visit(IOpenApiReferenceHolder referenceableHolder)
99
{
1010
base.Visit(referenceableHolder);
11-
if (referenceableHolder is OpenApiSchemaReference schemaReference && !string.IsNullOrEmpty(schemaReference.Reference?.Id) && schemaReference.Reference?.Id is not null)
11+
if (referenceableHolder is OpenApiSchemaReference schemaReference &&
12+
!string.IsNullOrEmpty(schemaReference.Reference?.Id) &&
13+
schemaReference.Reference?.Id is not null &&
14+
_visitedSchemasReferences.Add(schemaReference.Reference.Id) &&
15+
schemaReference.RecursiveTarget is {} referencedSchema)
1216
{
13-
_visitedSchemasReferences.Add(schemaReference.Reference.Id);
17+
Visit((IOpenApiSchema)referencedSchema);
18+
}
19+
}
20+
public override void Visit(IOpenApiSchema schema)
21+
{
22+
base.Visit(schema);
23+
if (schema is IOpenApiReferenceHolder referenceHolder) Visit(referenceHolder);
24+
if (schema is not OpenApiSchema openApiSchema) return;
25+
VisitCollection(openApiSchema.AllOf);
26+
VisitCollection(openApiSchema.AnyOf);
27+
VisitCollection(openApiSchema.OneOf);
28+
VisitCollection(openApiSchema.Properties?.Values);
29+
if (openApiSchema.AdditionalProperties is not null)
30+
Visit(openApiSchema.AdditionalProperties);
31+
if (openApiSchema.Items is not null)
32+
Visit(openApiSchema.Items);
33+
if (openApiSchema.Not is not null)
34+
Visit(openApiSchema.Not);
35+
if (openApiSchema.Discriminator?.Mapping is not null)
36+
VisitCollection(openApiSchema.Discriminator.Mapping.Values);
37+
//TODO add default mapping in V3
38+
}
39+
private void VisitCollection(IEnumerable<IOpenApiSchema>? schemas)
40+
{
41+
if (schemas is null) return;
42+
foreach (var schema in schemas)
43+
{
44+
Visit(schema);
1445
}
1546
}
1647
public HashSet<string> GetVisitedSchemasReferences()

0 commit comments

Comments
 (0)