Skip to content

Commit

Permalink
fix: passes missing host document references to all layers
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Dec 31, 2024
1 parent 9db6e2d commit ff1406c
Show file tree
Hide file tree
Showing 86 changed files with 183 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Reader/ParseNodes/ListNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ListNode(ParsingContext context, JsonArray jsonArray) : base(
_nodeList = jsonArray;
}

public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
{
if (_nodeList == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Reader/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PropertyNode this[string key]
}
}

public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
{
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
var nodes = jsonMap.Select(
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Reader/ParseNodes/ParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public static ParseNode Create(ParsingContext context, JsonNode node)
return new ValueNode(context, node as JsonValue);
}

public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
{
throw new OpenApiReaderException("Cannot create list from this type of node.", Context);
}

public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
{
throw new OpenApiReaderException("Cannot create map from this type of node.", Context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Reader/ParseNodes/PropertyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void ParseField<T>(
T parentInstance,
IDictionary<string, Action<T, ParseNode, OpenApiDocument>> fixedFields,
IDictionary<Func<string, bool>, Action<T, string, ParseNode, OpenApiDocument>> patternFields,
OpenApiDocument hostDocument = null)
OpenApiDocument hostDocument)
{
if (fixedFields.TryGetValue(Name, out var fixedFieldMap))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiContact LoadContact(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiContact LoadContact(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node as MapNode;
var contact = new OpenApiContact();
Expand Down
10 changes: 4 additions & 6 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,18 @@ internal static partial class OpenApiV2Deserializer
},
{
"parameters",
(o, n, _) =>
(o, n, doc) =>
{
if (o.Components == null)
{
o.Components = new();
}
o.Components ??= new();

o.Components.Parameters = n.CreateMap(LoadParameter, o);

o.Components.RequestBodies = n.CreateMap((p, d) =>
{
var parameter = LoadParameter(node: p, loadRequestBody: true, hostDocument: d);
return parameter != null ? CreateRequestBody(p.Context, parameter) : null;
}
},
doc
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiExternalDocs LoadExternalDocs(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiExternalDocs LoadExternalDocs(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("externalDocs");

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static partial class OpenApiV2Deserializer
},
{
"items",
(o, n, _) => GetOrCreateSchema(o).Items = LoadSchema(n)
(o, n, doc) => GetOrCreateSchema(o).Items = LoadSchema(n, doc)
},
{
"collectionFormat",
Expand Down Expand Up @@ -102,14 +102,14 @@ private static OpenApiSchema GetOrCreateSchema(OpenApiHeader p)
return p.Schema ??= new();
}

public static OpenApiHeader LoadHeader(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiHeader LoadHeader(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("header");
var header = new OpenApiHeader();

foreach (var property in mapNode)
{
property.ParseField(header, _headerFixedFields, _headerPatternFields);
property.ParseField(header, _headerFixedFields, _headerPatternFields, hostDocument);
}

var schema = node.Context.GetFromTempStorage<OpenApiSchema>("schema");
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Reader/V2/OpenApiInfoDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiInfo LoadInfo(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiInfo LoadInfo(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("Info");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiLicense LoadLicense(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiLicense LoadLicense(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("OpenApiLicense");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument hostDocument = null)
internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument hostDocument)
{
// Reset these temp storage parameters for each operation.
node.Context.SetTempStorage(TempStorageKeys.BodyParameter, null);
Expand Down Expand Up @@ -131,7 +131,7 @@ internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument h
return operation;
}

public static OpenApiResponses LoadResponses(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiResponses LoadResponses(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("Responses");

Expand Down Expand Up @@ -205,7 +205,7 @@ internal static OpenApiRequestBody CreateRequestBody(
}

private static OpenApiTagReference LoadTagByReference(
string tagName, OpenApiDocument hostDocument = null)
string tagName, OpenApiDocument hostDocument)
{
return new OpenApiTagReference(tagName, hostDocument);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static partial class OpenApiV2Deserializer
},
{
"items",
(o, n, t) => GetOrCreateSchema(o).Items = LoadSchema(n)
(o, n, t) => GetOrCreateSchema(o).Items = LoadSchema(n, t)
},
{
"collectionFormat",
Expand Down Expand Up @@ -138,7 +138,7 @@ private static void LoadStyle(OpenApiParameter p, string v)
}
}

private static void LoadParameterExamplesExtension(OpenApiParameter parameter, ParseNode node, OpenApiDocument hostDocument = null)
private static void LoadParameterExamplesExtension(OpenApiParameter parameter, ParseNode node, OpenApiDocument hostDocument)
{
var examples = LoadExamplesExtension(node);
node.Context.SetTempStorage(TempStorageKeys.Examples, examples, parameter);
Expand All @@ -149,7 +149,7 @@ private static OpenApiSchema GetOrCreateSchema(OpenApiParameter p)
return p.Schema ??= new();
}

private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument hostDocument = null)
private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument hostDocument)
{
var value = n.GetScalarValue();
switch (value)
Expand Down Expand Up @@ -180,12 +180,12 @@ private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument h
}
}

public static OpenApiParameter LoadParameter(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiParameter LoadParameter(ParseNode node, OpenApiDocument hostDocument)
{
return LoadParameter(node, false, hostDocument);
}

public static OpenApiParameter LoadParameter(ParseNode node, bool loadRequestBody, OpenApiDocument hostDocument = null)
public static OpenApiParameter LoadParameter(ParseNode node, bool loadRequestBody, OpenApiDocument hostDocument)
{
// Reset the local variables every time this method is called.
node.Context.SetTempStorage(TempStorageKeys.ParameterIsBodyOrFormData, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))},
};

public static OpenApiPathItem LoadPathItem(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiPathItem LoadPathItem(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("PathItem");

Expand All @@ -54,12 +54,12 @@ public static OpenApiPathItem LoadPathItem(ParseNode node, OpenApiDocument hostD
return pathItem;
}

private static void LoadPathParameters(OpenApiPathItem pathItem, ParseNode node, OpenApiDocument hostDocument = null)
private static void LoadPathParameters(OpenApiPathItem pathItem, ParseNode node, OpenApiDocument hostDocument)
{
node.Context.SetTempStorage(TempStorageKeys.BodyParameter, null);
node.Context.SetTempStorage(TempStorageKeys.FormParameters, null);

pathItem.Parameters = node.CreateList(LoadParameter);
pathItem.Parameters = node.CreateList(LoadParameter, hostDocument);

// Build request body based on information determined while parsing OpenApiOperation
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>(TempStorageKeys.BodyParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiPaths LoadPaths(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiPaths LoadPaths(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("Paths");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
}

private static void LoadResponseExamplesExtension(OpenApiResponse response, ParseNode node, OpenApiDocument hostDocument = null)
private static void LoadResponseExamplesExtension(OpenApiResponse response, ParseNode node, OpenApiDocument hostDocument)
{
var examples = LoadExamplesExtension(node);
node.Context.SetTempStorage(TempStorageKeys.Examples, examples, response);
Expand Down Expand Up @@ -145,7 +145,7 @@ private static Dictionary<string, OpenApiExample> LoadExamplesExtension(ParseNod
return examples;
}

private static void LoadExamples(OpenApiResponse response, ParseNode node, OpenApiDocument hostDocument = null)
private static void LoadExamples(OpenApiResponse response, ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("examples");

Expand Down Expand Up @@ -178,7 +178,7 @@ private static void LoadExample(OpenApiResponse response, string mediaType, Pars
mediaTypeObject.Example = exampleNode;
}

public static OpenApiResponse LoadResponse(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiResponse LoadResponse(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("response");

Expand All @@ -193,7 +193,7 @@ public static OpenApiResponse LoadResponse(ParseNode node, OpenApiDocument hostD

foreach (var property in mapNode)
{
property.ParseField(response, _responseFixedFields, _responsePatternFields);
property.ParseField(response, _responseFixedFields, _responsePatternFields, hostDocument);
}

foreach (var mediaType in response.Content.Values)
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,22 @@ internal static partial class OpenApiV2Deserializer
},
{
"items",
(o, n, _) => o.Items = LoadSchema(n)
(o, n, doc) => o.Items = LoadSchema(n, doc)
},
{
"properties",
(o, n, t) => o.Properties = n.CreateMap(LoadSchema, t)
},
{
"additionalProperties", (o, n, _) =>
"additionalProperties", (o, n, doc) =>
{
if (n is ValueNode)
{
o.AdditionalPropertiesAllowed = bool.Parse(n.GetScalarValue());
}
else
{
o.AdditionalProperties = LoadSchema(n);
o.AdditionalProperties = LoadSchema(n, doc);
}
}
},
Expand Down Expand Up @@ -139,11 +139,11 @@ internal static partial class OpenApiV2Deserializer
},
{
"xml",
(o, n, _) => o.Xml = LoadXml(n)
(o, n, doc) => o.Xml = LoadXml(n, doc)
},
{
"externalDocs",
(o, n, _) => o.ExternalDocs = LoadExternalDocs(n)
(o, n, doc) => o.ExternalDocs = LoadExternalDocs(n, doc)
},
{
"example",
Expand All @@ -156,7 +156,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("schema");

Expand All @@ -171,7 +171,7 @@ public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocum

foreach (var propertyNode in mapNode)
{
propertyNode.ParseField(schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields);
propertyNode.ParseField(schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields, hostDocument);
}

return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node, OpenApiDocument hostDocument)
{
// Reset the local variables every time this method is called.
// TODO: Change _flow to a tempStorage variable to make the deserializer thread-safe.

Check warning on line 74 in src/Microsoft.OpenApi/Reader/V2/OpenApiSecuritySchemeDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
Expand All @@ -80,7 +80,7 @@ public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node, OpenApiDo
var securityScheme = new OpenApiSecurityScheme();
foreach (var property in mapNode)
{
property.ParseField(securityScheme, _securitySchemeFixedFields, _securitySchemePatternFields);
property.ParseField(securityScheme, _securitySchemeFixedFields, _securitySchemePatternFields, hostDocument);
}

// Put the Flow object in the right Flows property based on the string in "flow"
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiTagDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiTag LoadTag(ParseNode n, OpenApiDocument hostDocument = null)
public static OpenApiTag LoadTag(ParseNode n, OpenApiDocument hostDocument)
{
var mapNode = n.CheckMapNode("tag");

var domainObject = new OpenApiTag();

foreach (var propertyNode in mapNode)
{
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields);
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields, hostDocument);
}

return domainObject;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Reader/V2/OpenApiV2Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void ProcessAnyFields<T>(
}
}

public static JsonNode LoadAny(ParseNode node, OpenApiDocument hostDocument = null)
public static JsonNode LoadAny(ParseNode node, OpenApiDocument hostDocument)
{
return node.CreateAny();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
};

public static OpenApiXml LoadXml(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiXml LoadXml(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("xml");

var xml = new OpenApiXml();
foreach (var property in mapNode)
{
property.ParseField(xml, _xmlFixedFields, _xmlPatternFields);
property.ParseField(xml, _xmlFixedFields, _xmlPatternFields, hostDocument);
}

return xml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static partial class OpenApiV3Deserializer
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))},
};

public static OpenApiCallback LoadCallback(ParseNode node, OpenApiDocument hostDocument = null)
public static OpenApiCallback LoadCallback(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("callback");

Expand Down
Loading

0 comments on commit ff1406c

Please sign in to comment.