Skip to content

Commit

Permalink
feat: Make REQUIRED properties as non-nullable and revert some change…
Browse files Browse the repository at this point in the history
…s according this.
  • Loading branch information
HavenDV committed Sep 5, 2024
1 parent 7e0f1e0 commit 45b0e5e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger,
stopwatch.Start();
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
stopwatch.Stop();
logger.LogTrace("{Timestamp}ms: Creating filtered OpenApi document with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths?.Count);
logger.LogTrace("{Timestamp}ms: Creating filtered OpenApi document with {Paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
}

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

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

writer.WriteLine("# " + document.Info?.Title);
writer.WriteLine("# " + document.Info.Title);
writer.WriteLine();
writer.WriteLine("API Description: " + openapiUrl);

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

Expand Down Expand Up @@ -773,8 +773,8 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
var manifest = new OpenAIPluginManifest
{
NameForHuman = document.Info?.Title,
DescriptionForHuman = document.Info?.Description,
NameForHuman = document.Info.Title,
DescriptionForHuman = document.Info.Description,
Api = new()
{
Type = "openapi",
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// REQUIRED. Provides metadata about the API. The metadata MAY be used by tooling as required.
/// </summary>
public OpenApiInfo? Info { get; set; }
public OpenApiInfo Info { get; set; }

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

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

/// <summary>
Expand All @@ -108,10 +110,10 @@ public OpenApiDocument()
public OpenApiDocument(OpenApiDocument? document)
{
Workspace = document?.Workspace != null ? new(document?.Workspace) : null;
Info = document?.Info != null ? new(document?.Info) : null;
Info = document?.Info != null ? new(document?.Info) : new OpenApiInfo();
JsonSchemaDialect = document?.JsonSchemaDialect ?? JsonSchemaDialect;
Servers = document?.Servers != null ? new List<OpenApiServer>(document.Servers) : null;
Paths = document?.Paths != null ? new(document?.Paths) : null;
Paths = document?.Paths != null ? new(document?.Paths) : new OpenApiPaths();
Webhooks = document?.Webhooks != null ? new Dictionary<string, OpenApiPathItem>(document.Webhooks) : null;
Components = document?.Components != null ? new(document?.Components) : null;
SecurityRequirements = document?.SecurityRequirements != null ? new List<OpenApiSecurityRequirement>(document.SecurityRequirements) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public void ResolveFunctionParameters()
var walker = new OpenApiWalker(powerShellFormatter);
walker.Walk(openApiDocument);

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

// Assert
Assert.Null(idsParameter?.Content);
Assert.NotNull(idsParameter?.Schema);
Assert.Equal("array", idsParameter.Schema.Type);
Assert.Equal("array", idsParameter?.Schema.Type);
}

private static OpenApiDocument GetSampleOpenApiDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void ReturnFilteredOpenApiDocumentBasedOnOperationIdsAndTags(string? oper

// Assert
Assert.NotNull(subsetOpenApiDocument);
Assert.NotNull(subsetOpenApiDocument.Paths);
Assert.NotEmpty(subsetOpenApiDocument.Paths);
Assert.Equal(expectedPathCount, subsetOpenApiDocument.Paths.Count);
}
Expand All @@ -63,7 +62,6 @@ public void ReturnFilteredOpenApiDocumentBasedOnPostmanCollection()

// Assert
Assert.NotNull(subsetOpenApiDocument);
Assert.NotNull(subsetOpenApiDocument.Paths);
Assert.NotEmpty(subsetOpenApiDocument.Paths);
Assert.Equal(3, subsetOpenApiDocument.Paths.Count);
}
Expand Down Expand Up @@ -152,11 +150,10 @@ public void ContinueProcessingWhenUrlsInCollectionAreMissingFromSourceDocument()
var pathCount = requestUrls.Count;
var predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: _openApiDocumentMock);
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate);
var subsetPathCount = subsetOpenApiDocument.Paths?.Count;
var subsetPathCount = subsetOpenApiDocument.Paths.Count;

// Assert
Assert.NotNull(subsetOpenApiDocument);
Assert.NotNull(subsetOpenApiDocument.Paths);
Assert.NotEmpty(subsetOpenApiDocument.Paths);
Assert.Equal(2, subsetPathCount);
Assert.NotEqual(pathCount, subsetPathCount);
Expand All @@ -183,7 +180,6 @@ public void ReturnsPathParametersOnSlicingBasedOnOperationIdsOrTags(string? oper
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate);

// Assert
Assert.NotNull(subsetOpenApiDocument.Paths);
foreach (var pathItem in subsetOpenApiDocument.Paths)
{
Assert.True(pathItem.Value.Parameters.Any());
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ namespace Microsoft.OpenApi.Models
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiExtension>? Extensions { get; set; }
public Microsoft.OpenApi.Models.OpenApiExternalDocs? ExternalDocs { get; set; }
public string HashCode { get; }
public Microsoft.OpenApi.Models.OpenApiInfo? Info { get; set; }
public Microsoft.OpenApi.Models.OpenApiInfo Info { get; set; }
public string? JsonSchemaDialect { get; set; }
public Microsoft.OpenApi.Models.OpenApiPaths? Paths { get; set; }
public Microsoft.OpenApi.Models.OpenApiPaths Paths { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSecurityRequirement>? SecurityRequirements { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiServer>? Servers { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiTag>? Tags { get; set; }
Expand Down
12 changes: 6 additions & 6 deletions test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public void LocateTopLevelObjects()
walker.Walk(doc);

locator.Locations.Should().BeEquivalentTo(new List<string> {
"#/info",
"#/servers",
"#/paths",
"#/tags"
});
}
Expand All @@ -39,7 +41,6 @@ public void LocateTopLevelArrayItems()
new(),
new()
},
Paths = new(),
Tags = new List<OpenApiTag>
{
new()
Expand All @@ -51,6 +52,7 @@ public void LocateTopLevelArrayItems()
walker.Walk(doc);

locator.Locations.Should().BeEquivalentTo(new List<string> {
"#/info",
"#/servers",
"#/servers/0",
"#/servers/1",
Expand All @@ -63,10 +65,7 @@ public void LocateTopLevelArrayItems()
[Fact]
public void LocatePathOperationContentSchema()
{
var doc = new OpenApiDocument
{
Paths = new()
};
var doc = new OpenApiDocument();
doc.Paths.Add("/test", new()
{
Operations = new Dictionary<OperationType, OpenApiOperation>
Expand Down Expand Up @@ -98,6 +97,7 @@ public void LocatePathOperationContentSchema()
walker.Walk(doc);

locator.Locations.Should().BeEquivalentTo(new List<string> {
"#/info",
"#/servers",
"#/paths",
"#/paths/~1test",
Expand Down Expand Up @@ -131,7 +131,6 @@ public void WalkDOMWithCycles()

var doc = new OpenApiDocument
{
Paths = new(),
Components = new()
{
Schemas = new Dictionary<string, OpenApiSchema>
Expand All @@ -146,6 +145,7 @@ public void WalkDOMWithCycles()
walker.Walk(doc);

locator.Locations.Should().BeEquivalentTo(new List<string> {
"#/info",
"#/servers",
"#/paths",
"#/components",
Expand Down

0 comments on commit 45b0e5e

Please sign in to comment.