Skip to content

Commit cd8d13b

Browse files
Avoid allocating segment
Avoid allocating the segment for the context if the reference is valid.
1 parent 983457c commit cd8d13b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Microsoft.OpenApi/Validations/Rules/OpenApiDocumentRules.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,6 @@ public override void Visit(IOpenApiSchema schema)
8181

8282
private void ValidateSchemaReference(OpenApiSchemaReference reference)
8383
{
84-
// Trim off the leading "#/" as the context is already at the root of the document
85-
var segment =
86-
#if NET8_0_OR_GREATER
87-
$"{PathString[2..]}/$ref";
88-
#else
89-
PathString.Substring(2) + "/$ref";
90-
#endif
91-
9284
try
9385
{
9486
if (reference.RecursiveTarget is not null)
@@ -99,7 +91,7 @@ private void ValidateSchemaReference(OpenApiSchemaReference reference)
9991
}
10092
catch (InvalidOperationException ex)
10193
{
102-
context.Enter(segment);
94+
context.Enter(GetSegment());
10395
context.CreateWarning(ruleName, ex.Message);
10496
context.Exit();
10597

@@ -129,7 +121,7 @@ private void ValidateSchemaReference(OpenApiSchemaReference reference)
129121

130122
if (!isValid)
131123
{
132-
context.Enter(segment);
124+
context.Enter(GetSegment());
133125
context.CreateWarning(ruleName, string.Format(SRResource.Validation_SchemaReferenceDoesNotExist, id));
134126
context.Exit();
135127
}
@@ -143,6 +135,17 @@ static bool IsValidSchemaReference(string id, JsonNode baseNode)
143135
var pointer = new JsonPointer(id.Replace("#/", "/"));
144136
return pointer.Find(baseNode);
145137
}
138+
139+
string GetSegment()
140+
{
141+
// Trim off the leading "#/" as the context is already at the root of the document
142+
return
143+
#if NET8_0_OR_GREATER
144+
$"{PathString[2..]}/$ref";
145+
#else
146+
PathString.Substring(2) + "/$ref";
147+
#endif
148+
}
146149
}
147150
}
148151
}

0 commit comments

Comments
 (0)