Skip to content

Commit 45b0e5e

Browse files
committed
feat: Make REQUIRED properties as non-nullable and revert some changes according this.
1 parent 7e0f1e0 commit 45b0e5e

File tree

6 files changed

+23
-25
lines changed

6 files changed

+23
-25
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger,
185185
stopwatch.Start();
186186
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
187187
stopwatch.Stop();
188-
logger.LogTrace("{Timestamp}ms: Creating filtered OpenApi document with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths?.Count);
188+
logger.LogTrace("{Timestamp}ms: Creating filtered OpenApi document with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
189189
}
190190

191191
return document;
@@ -248,7 +248,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, strin
248248

249249
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, format, metadataVersion, options.SettingsConfig, cancellationToken).ConfigureAwait(false);
250250
stopwatch.Stop();
251-
logger.LogTrace("{Timestamp}ms: Generated OpenAPI with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths?.Count);
251+
logger.LogTrace("{Timestamp}ms: Generated OpenAPI with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
252252
}
253253
}
254254
else if (!string.IsNullOrEmpty(options.OpenApi))
@@ -666,7 +666,7 @@ internal static void WriteTreeDocumentAsMarkdown(string openapiUrl, OpenApiDocum
666666
{
667667
var rootNode = OpenApiUrlTreeNode.Create(document, "main");
668668

669-
writer.WriteLine("# " + document.Info?.Title);
669+
writer.WriteLine("# " + document.Info.Title);
670670
writer.WriteLine();
671671
writer.WriteLine("API Description: " + openapiUrl);
672672

@@ -702,7 +702,7 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
702702
</style>
703703
<body>
704704
""");
705-
writer.WriteLine("<h1>" + document.Info?.Title + "</h1>");
705+
writer.WriteLine("<h1>" + document.Info.Title + "</h1>");
706706
writer.WriteLine();
707707
writer.WriteLine($"<h3> API Description: <a href='{sourceUrl}'>{sourceUrl}</a></h3>");
708708

@@ -773,8 +773,8 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
773773
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
774774
var manifest = new OpenAIPluginManifest
775775
{
776-
NameForHuman = document.Info?.Title,
777-
DescriptionForHuman = document.Info?.Description,
776+
NameForHuman = document.Info.Title,
777+
DescriptionForHuman = document.Info.Description,
778778
Api = new()
779779
{
780780
Type = "openapi",

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
3333
/// <summary>
3434
/// REQUIRED. Provides metadata about the API. The metadata MAY be used by tooling as required.
3535
/// </summary>
36-
public OpenApiInfo? Info { get; set; }
36+
public OpenApiInfo Info { get; set; }
3737

3838
/// <summary>
3939
/// The default value for the $schema keyword within Schema Objects contained within this OAS document. This MUST be in the form of a URI.
@@ -48,7 +48,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
4848
/// <summary>
4949
/// REQUIRED. The available paths and operations for the API.
5050
/// </summary>
51-
public OpenApiPaths? Paths { get; set; }
51+
public OpenApiPaths Paths { get; set; }
5252

5353
/// <summary>
5454
/// The incoming webhooks that MAY be received as part of this API and that the API consumer MAY choose to implement.
@@ -100,6 +100,8 @@ public OpenApiDocument()
100100
{
101101
Workspace = new OpenApiWorkspace();
102102
BaseUri = new(OpenApiConstants.BaseRegistryUri + Guid.NewGuid());
103+
Info = new OpenApiInfo();
104+
Paths = new OpenApiPaths();
103105
}
104106

105107
/// <summary>
@@ -108,10 +110,10 @@ public OpenApiDocument()
108110
public OpenApiDocument(OpenApiDocument? document)
109111
{
110112
Workspace = document?.Workspace != null ? new(document?.Workspace) : null;
111-
Info = document?.Info != null ? new(document?.Info) : null;
113+
Info = document?.Info != null ? new(document?.Info) : new OpenApiInfo();
112114
JsonSchemaDialect = document?.JsonSchemaDialect ?? JsonSchemaDialect;
113115
Servers = document?.Servers != null ? new List<OpenApiServer>(document.Servers) : null;
114-
Paths = document?.Paths != null ? new(document?.Paths) : null;
116+
Paths = document?.Paths != null ? new(document?.Paths) : new OpenApiPaths();
115117
Webhooks = document?.Webhooks != null ? new Dictionary<string, OpenApiPathItem>(document.Webhooks) : null;
116118
Components = document?.Components != null ? new(document?.Components) : null;
117119
SecurityRequirements = document?.SecurityRequirements != null ? new List<OpenApiSecurityRequirement>(document.SecurityRequirements) : null;

test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ public void ResolveFunctionParameters()
8686
var walker = new OpenApiWalker(powerShellFormatter);
8787
walker.Walk(openApiDocument);
8888

89-
var idsParameter = openApiDocument.Paths?["/foo"].Operations[OperationType.Get].Parameters?.Where(static p => p.Name == "ids").FirstOrDefault();
89+
var idsParameter = openApiDocument.Paths["/foo"].Operations[OperationType.Get].Parameters?.Where(static p => p.Name == "ids").FirstOrDefault();
9090

9191
// Assert
9292
Assert.Null(idsParameter?.Content);
9393
Assert.NotNull(idsParameter?.Schema);
94-
Assert.Equal("array", idsParameter.Schema.Type);
94+
Assert.Equal("array", idsParameter?.Schema.Type);
9595
}
9696

9797
private static OpenApiDocument GetSampleOpenApiDocument()

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public void ReturnFilteredOpenApiDocumentBasedOnOperationIdsAndTags(string? oper
4343

4444
// Assert
4545
Assert.NotNull(subsetOpenApiDocument);
46-
Assert.NotNull(subsetOpenApiDocument.Paths);
4746
Assert.NotEmpty(subsetOpenApiDocument.Paths);
4847
Assert.Equal(expectedPathCount, subsetOpenApiDocument.Paths.Count);
4948
}
@@ -63,7 +62,6 @@ public void ReturnFilteredOpenApiDocumentBasedOnPostmanCollection()
6362

6463
// Assert
6564
Assert.NotNull(subsetOpenApiDocument);
66-
Assert.NotNull(subsetOpenApiDocument.Paths);
6765
Assert.NotEmpty(subsetOpenApiDocument.Paths);
6866
Assert.Equal(3, subsetOpenApiDocument.Paths.Count);
6967
}
@@ -152,11 +150,10 @@ public void ContinueProcessingWhenUrlsInCollectionAreMissingFromSourceDocument()
152150
var pathCount = requestUrls.Count;
153151
var predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: _openApiDocumentMock);
154152
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate);
155-
var subsetPathCount = subsetOpenApiDocument.Paths?.Count;
153+
var subsetPathCount = subsetOpenApiDocument.Paths.Count;
156154

157155
// Assert
158156
Assert.NotNull(subsetOpenApiDocument);
159-
Assert.NotNull(subsetOpenApiDocument.Paths);
160157
Assert.NotEmpty(subsetOpenApiDocument.Paths);
161158
Assert.Equal(2, subsetPathCount);
162159
Assert.NotEqual(pathCount, subsetPathCount);
@@ -183,7 +180,6 @@ public void ReturnsPathParametersOnSlicingBasedOnOperationIdsOrTags(string? oper
183180
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate);
184181

185182
// Assert
186-
Assert.NotNull(subsetOpenApiDocument.Paths);
187183
foreach (var pathItem in subsetOpenApiDocument.Paths)
188184
{
189185
Assert.True(pathItem.Value.Parameters.Any());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,9 @@ namespace Microsoft.OpenApi.Models
537537
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiExtension>? Extensions { get; set; }
538538
public Microsoft.OpenApi.Models.OpenApiExternalDocs? ExternalDocs { get; set; }
539539
public string HashCode { get; }
540-
public Microsoft.OpenApi.Models.OpenApiInfo? Info { get; set; }
540+
public Microsoft.OpenApi.Models.OpenApiInfo Info { get; set; }
541541
public string? JsonSchemaDialect { get; set; }
542-
public Microsoft.OpenApi.Models.OpenApiPaths? Paths { get; set; }
542+
public Microsoft.OpenApi.Models.OpenApiPaths Paths { get; set; }
543543
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSecurityRequirement>? SecurityRequirements { get; set; }
544544
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiServer>? Servers { get; set; }
545545
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiTag>? Tags { get; set; }

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public void LocateTopLevelObjects()
2424
walker.Walk(doc);
2525

2626
locator.Locations.Should().BeEquivalentTo(new List<string> {
27+
"#/info",
2728
"#/servers",
29+
"#/paths",
2830
"#/tags"
2931
});
3032
}
@@ -39,7 +41,6 @@ public void LocateTopLevelArrayItems()
3941
new(),
4042
new()
4143
},
42-
Paths = new(),
4344
Tags = new List<OpenApiTag>
4445
{
4546
new()
@@ -51,6 +52,7 @@ public void LocateTopLevelArrayItems()
5152
walker.Walk(doc);
5253

5354
locator.Locations.Should().BeEquivalentTo(new List<string> {
55+
"#/info",
5456
"#/servers",
5557
"#/servers/0",
5658
"#/servers/1",
@@ -63,10 +65,7 @@ public void LocateTopLevelArrayItems()
6365
[Fact]
6466
public void LocatePathOperationContentSchema()
6567
{
66-
var doc = new OpenApiDocument
67-
{
68-
Paths = new()
69-
};
68+
var doc = new OpenApiDocument();
7069
doc.Paths.Add("/test", new()
7170
{
7271
Operations = new Dictionary<OperationType, OpenApiOperation>
@@ -98,6 +97,7 @@ public void LocatePathOperationContentSchema()
9897
walker.Walk(doc);
9998

10099
locator.Locations.Should().BeEquivalentTo(new List<string> {
100+
"#/info",
101101
"#/servers",
102102
"#/paths",
103103
"#/paths/~1test",
@@ -131,7 +131,6 @@ public void WalkDOMWithCycles()
131131

132132
var doc = new OpenApiDocument
133133
{
134-
Paths = new(),
135134
Components = new()
136135
{
137136
Schemas = new Dictionary<string, OpenApiSchema>
@@ -146,6 +145,7 @@ public void WalkDOMWithCycles()
146145
walker.Walk(doc);
147146

148147
locator.Locations.Should().BeEquivalentTo(new List<string> {
148+
"#/info",
149149
"#/servers",
150150
"#/paths",
151151
"#/components",

0 commit comments

Comments
 (0)