Skip to content

Commit 80edd66

Browse files
committed
fix: remove nullable Json node params
1 parent d990121 commit 80edd66

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static ReadResult Read(JsonNode jsonNode, OpenApiReaderSettings settings)
110110
}
111111

112112
/// <inheritdoc/>
113-
public static T ReadFragment<T>(JsonNode input, OpenApiSpecVersion version, OpenApiDocument openApiDocument, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement
113+
public static T? ReadFragment<T>(JsonNode input, OpenApiSpecVersion version, OpenApiDocument openApiDocument, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement
114114
{
115115
return _jsonReader.ReadFragment<T>(input, version, openApiDocument, out diagnostic, settings);
116116
}

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ public IOpenApiParameter ConvertToBodyParameter(IOpenApiWriter writer)
108108
Extensions = Extensions?.ToDictionary(static k => k.Key, static v => v.Value)
109109
};
110110
// Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
111-
if (bodyParameter.Extensions is not null && bodyParameter.Extensions.TryGetValue(OpenApiConstants.BodyName, out var bodyNameExtension))
111+
if (bodyParameter.Extensions is not null &&
112+
bodyParameter.Extensions.TryGetValue(OpenApiConstants.BodyName, out var bodyNameExtension) &&
113+
bodyNameExtension is OpenApiAny bodyName)
112114
{
113-
var bodyName = bodyNameExtension as OpenApiAny;
114-
bodyParameter.Name = string.IsNullOrEmpty(bodyName?.Node?.ToString()) ? "body" : bodyName?.Node?.ToString();
115+
bodyParameter.Name = string.IsNullOrEmpty(bodyName.Node.ToString()) ? "body" : bodyName.Node.ToString();
115116
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
116117
}
117118
return bodyParameter;

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ReadResult Read(MemoryStream input,
4040
// Parse the JSON text in the stream into JsonNodes
4141
try
4242
{
43-
jsonNode = JsonNode.Parse(input);
43+
jsonNode = JsonNode.Parse(input) ?? throw new InvalidOperationException($"Cannot parse input stream, {nameof(input)}.");
4444
}
4545
catch (JsonException ex)
4646
{
@@ -61,7 +61,7 @@ public ReadResult Read(MemoryStream input,
6161
/// <param name="jsonNode">The JsonNode input.</param>
6262
/// <param name="settings">The Reader settings to be used during parsing.</param>
6363
/// <returns></returns>
64-
public ReadResult Read(JsonNode? jsonNode,
64+
public ReadResult Read(JsonNode jsonNode,
6565
OpenApiReaderSettings settings)
6666
{
6767
if (jsonNode is null) throw new ArgumentNullException(nameof(jsonNode));
@@ -131,7 +131,8 @@ public async Task<ReadResult> ReadAsync(Stream input,
131131
// Parse the JSON text in the stream into JsonNodes
132132
try
133133
{
134-
jsonNode = await JsonNode.ParseAsync(input, cancellationToken: cancellationToken).ConfigureAwait(false);
134+
jsonNode = await JsonNode.ParseAsync(input, cancellationToken: cancellationToken).ConfigureAwait(false) ??
135+
throw new InvalidOperationException($"failed to parse input stream, {nameof(input)}");
135136
}
136137
catch (JsonException ex)
137138
{
@@ -156,12 +157,12 @@ public async Task<ReadResult> ReadAsync(Stream input,
156157
Utils.CheckArgumentNull(input);
157158
Utils.CheckArgumentNull(openApiDocument);
158159

159-
JsonNode? jsonNode;
160+
JsonNode jsonNode;
160161

161162
// Parse the JSON
162163
try
163164
{
164-
jsonNode = JsonNode.Parse(input);
165+
jsonNode = JsonNode.Parse(input) ?? throw new InvalidOperationException($"Failed to parse stream, {nameof(input)}");
165166
}
166167
catch (JsonException ex)
167168
{
@@ -174,11 +175,11 @@ public async Task<ReadResult> ReadAsync(Stream input,
174175
}
175176

176177
/// <inheritdoc/>
177-
public T ReadFragment<T>(JsonNode? input,
178-
OpenApiSpecVersion version,
179-
OpenApiDocument openApiDocument,
180-
out OpenApiDiagnostic diagnostic,
181-
OpenApiReaderSettings? settings = null) where T : IOpenApiElement
178+
public T? ReadFragment<T>(JsonNode input,
179+
OpenApiSpecVersion version,
180+
OpenApiDocument openApiDocument,
181+
out OpenApiDiagnostic diagnostic,
182+
OpenApiReaderSettings? settings = null) where T : IOpenApiElement
182183
{
183184
diagnostic = new();
184185
settings ??= new OpenApiReaderSettings();
@@ -211,7 +212,7 @@ public T ReadFragment<T>(JsonNode? input,
211212
}
212213
}
213214

214-
return (T?)element!;
215+
return (T?)element;
215216
}
216217
}
217218
}

src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ internal static IOpenApiRequestBody CreateRequestBody(
230230
Extensions = bodyParameter.Extensions
231231
};
232232

233-
if (requestBody.Extensions is not null)
233+
if (requestBody.Extensions is not null && bodyParameter.Name is not null)
234234
{
235235
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiAny(bodyParameter.Name);
236236
}

test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void BrokenSimpleList()
3333
var result = OpenApiDocument.Parse(input, "yaml");
3434

3535
Assert.Equivalent(new List<OpenApiError>() {
36-
new OpenApiError(new OpenApiReaderException("Expected a value."))
36+
new OpenApiError(new OpenApiReaderException("Expected a value while parsing at #/schemes."))
3737
}, result.Diagnostic.Errors);
3838
}
3939

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Microsoft.OpenApi.Any
77
{
88
public class OpenApiAny : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtension
99
{
10-
public OpenApiAny(System.Text.Json.Nodes.JsonNode? jsonNode) { }
11-
public System.Text.Json.Nodes.JsonNode? Node { get; }
10+
public OpenApiAny(System.Text.Json.Nodes.JsonNode jsonNode) { }
11+
public System.Text.Json.Nodes.JsonNode Node { get; }
1212
public void Write(Microsoft.OpenApi.Writers.IOpenApiWriter writer, Microsoft.OpenApi.OpenApiSpecVersion specVersion) { }
1313
}
1414
}
@@ -17,8 +17,8 @@ namespace Microsoft.OpenApi.Attributes
1717
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field)]
1818
public class DisplayAttribute : System.Attribute
1919
{
20-
public DisplayAttribute(string? name) { }
21-
public string? Name { get; }
20+
public DisplayAttribute(string name) { }
21+
public string Name { get; }
2222
}
2323
}
2424
namespace Microsoft.OpenApi.Exceptions
@@ -37,14 +37,13 @@ namespace Microsoft.OpenApi.Exceptions
3737
public OpenApiReaderException(string message) { }
3838
public OpenApiReaderException(string message, Microsoft.OpenApi.Reader.ParsingContext context) { }
3939
public OpenApiReaderException(string message, System.Exception innerException) { }
40-
public OpenApiReaderException(string message, System.Text.Json.Nodes.JsonNode? node) { }
4140
}
4241
[System.Serializable]
4342
public class OpenApiUnsupportedSpecVersionException : System.Exception
4443
{
45-
public OpenApiUnsupportedSpecVersionException(string? specificationVersion) { }
44+
public OpenApiUnsupportedSpecVersionException(string specificationVersion) { }
4645
public OpenApiUnsupportedSpecVersionException(string specificationVersion, System.Exception innerException) { }
47-
public string? SpecificationVersion { get; }
46+
public string SpecificationVersion { get; }
4847
}
4948
public class OpenApiWriterException : Microsoft.OpenApi.Exceptions.OpenApiException
5049
{
@@ -1246,7 +1245,7 @@ namespace Microsoft.OpenApi.Models.References
12461245
where V : Microsoft.OpenApi.Interfaces.IOpenApiSerializable
12471246
{
12481247
protected BaseOpenApiReferenceHolder(Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder<T, V> source) { }
1249-
protected BaseOpenApiReferenceHolder(string? referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument, Microsoft.OpenApi.Models.ReferenceType referenceType, string? externalResource) { }
1248+
protected BaseOpenApiReferenceHolder(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument, Microsoft.OpenApi.Models.ReferenceType referenceType, string? externalResource) { }
12501249
public Microsoft.OpenApi.Models.OpenApiReference Reference { get; init; }
12511250
public virtual T Target { get; }
12521251
public bool UnresolvedReference { get; }
@@ -1310,7 +1309,7 @@ namespace Microsoft.OpenApi.Models.References
13101309
}
13111310
public class OpenApiParameterReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder<Microsoft.OpenApi.Models.OpenApiParameter, Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter>, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Interfaces.IShallowCopyable<Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter>, Microsoft.OpenApi.Models.Interfaces.IOpenApiDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiParameter
13121311
{
1313-
public OpenApiParameterReference(string? referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument = null, string? externalResource = null) { }
1312+
public OpenApiParameterReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument = null, string? externalResource = null) { }
13141313
public bool AllowEmptyValue { get; }
13151314
public bool AllowReserved { get; }
13161315
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiMediaType>? Content { get; }
@@ -1444,7 +1443,7 @@ namespace Microsoft.OpenApi.Models.References
14441443
}
14451444
public class OpenApiTagReference : Microsoft.OpenApi.Models.References.BaseOpenApiReferenceHolder<Microsoft.OpenApi.Models.OpenApiTag, Microsoft.OpenApi.Models.Interfaces.IOpenApiTag>, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiReadOnlyExtensible, Microsoft.OpenApi.Interfaces.IOpenApiSerializable, Microsoft.OpenApi.Interfaces.IShallowCopyable<Microsoft.OpenApi.Models.Interfaces.IOpenApiTag>, Microsoft.OpenApi.Models.Interfaces.IOpenApiReadOnlyDescribedElement, Microsoft.OpenApi.Models.Interfaces.IOpenApiTag
14461445
{
1447-
public OpenApiTagReference(string? referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument = null, string? externalResource = null) { }
1446+
public OpenApiTagReference(string referenceId, Microsoft.OpenApi.Models.OpenApiDocument? hostDocument = null, string? externalResource = null) { }
14481447
public string? Description { get; }
14491448
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiExtension>? Extensions { get; }
14501449
public Microsoft.OpenApi.Models.OpenApiExternalDocs? ExternalDocs { get; }
@@ -1468,11 +1467,11 @@ namespace Microsoft.OpenApi.Reader
14681467
{
14691468
public OpenApiJsonReader() { }
14701469
public Microsoft.OpenApi.Reader.ReadResult Read(System.IO.MemoryStream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings) { }
1471-
public Microsoft.OpenApi.Reader.ReadResult Read(System.Text.Json.Nodes.JsonNode? jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings) { }
1470+
public Microsoft.OpenApi.Reader.ReadResult Read(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings) { }
14721471
public System.Threading.Tasks.Task<Microsoft.OpenApi.Reader.ReadResult> ReadAsync(System.IO.Stream input, Microsoft.OpenApi.Reader.OpenApiReaderSettings settings, System.Threading.CancellationToken cancellationToken = default) { }
14731472
public T? ReadFragment<T>(System.IO.MemoryStream input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Models.OpenApiDocument openApiDocument, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null)
14741473
where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { }
1475-
public T ReadFragment<T>(System.Text.Json.Nodes.JsonNode? input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Models.OpenApiDocument openApiDocument, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null)
1474+
public T? ReadFragment<T>(System.Text.Json.Nodes.JsonNode input, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Models.OpenApiDocument openApiDocument, out Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnostic, Microsoft.OpenApi.Reader.OpenApiReaderSettings? settings = null)
14761475
where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { }
14771476
}
14781477
public static class OpenApiModelFactory
@@ -1525,7 +1524,7 @@ namespace Microsoft.OpenApi.Reader
15251524
public T? GetFromTempStorage<T>(string key, object? scope = null) { }
15261525
public string GetLocation() { }
15271526
public Microsoft.OpenApi.Models.OpenApiDocument Parse(System.Text.Json.Nodes.JsonNode jsonNode) { }
1528-
public T? ParseFragment<T>(System.Text.Json.Nodes.JsonNode? jsonNode, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Models.OpenApiDocument openApiDocument)
1527+
public T? ParseFragment<T>(System.Text.Json.Nodes.JsonNode jsonNode, Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Models.OpenApiDocument openApiDocument)
15291528
where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { }
15301529
public void PopLoop(string loopid) { }
15311530
public bool PushLoop(string loopId, string key) { }

0 commit comments

Comments
 (0)