Skip to content

Commit

Permalink
Merge pull request #4367 from microsoft/bugfix/no-cotent-additional-c…
Browse files Browse the repository at this point in the history
…odes

- fixes a bug where no content codes would be considered structured
  • Loading branch information
baywet authored Mar 21, 2024
2 parents 45ef2c9 + ee91375 commit 8b65340
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- PREVIEW: Renamed the config to workspace file. [#4310](https://github.com/microsoft/kiota/issues/4310)
- Fixed a bug where TypeScript generation would consider boolean argument as empty when false. [#4103](https://github.com/microsoft/kiota/issues/4103)
- Changed Csharp code generation to put braces on new lines (where it makes sense). [#4347](https://github.com/microsoft/kiota/issues/4347)
- Fixed a bug where some no-content status codes would be considered structured (301, 302, 303, 307) when described. [#4190](https://github.com/microsoft/kiota/issues/4190)
- TypeScript is now a preview language!

## [1.12.0] - 2024-03-06
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ openApiExtension is OpenApiPrimaryErrorMessageExtension primaryErrorMessageExten
};
}
private const string RequestBodyPlainTextContentType = "text/plain";
private static readonly HashSet<string> noContentStatusCodes = new(StringComparer.OrdinalIgnoreCase) { "201", "202", "204", "205" };
private static readonly HashSet<string> noContentStatusCodes = new(StringComparer.OrdinalIgnoreCase) { "201", "202", "204", "205", "301", "302", "303", "304", "307" };
private static readonly HashSet<string> errorStatusCodes = new(Enumerable.Range(400, 599).Select(static x => x.ToString(CultureInfo.InvariantCulture))
.Concat([CodeMethod.ErrorMappingClientRange, CodeMethod.ErrorMappingServerRange]), StringComparer.OrdinalIgnoreCase);
private void AddErrorMappingsForExecutorMethod(OpenApiUrlTreeNode currentNode, OpenApiOperation operation, CodeMethod executorMethod)
Expand Down
83 changes: 80 additions & 3 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5411,8 +5411,14 @@ public void Considers204WithNoSchemaOver206WithNoSchema()
Assert.NotNull(executor);
Assert.Equal("void", executor.ReturnType.Name);
}
[Fact]
public void DoesntGenerateVoidExecutorOnMixed204()
[InlineData(204)]
[InlineData(301)]
[InlineData(302)]
[InlineData(303)]
[InlineData(304)]
[InlineData(307)]
[Theory]
public void DoesntGenerateVoidExecutorOnMixedNoContent(int statusCode)
{
var myObjectSchema = new OpenApiSchema
{
Expand Down Expand Up @@ -5449,7 +5455,7 @@ public void DoesntGenerateVoidExecutorOnMixed204()
}
}
},
["204"] = new OpenApiResponse(),
[statusCode.ToString()] = new OpenApiResponse(),
}
}
}
Expand Down Expand Up @@ -5477,6 +5483,77 @@ public void DoesntGenerateVoidExecutorOnMixed204()
Assert.NotNull(executor);
Assert.NotEqual("void", executor.ReturnType.Name);
}
[InlineData(204)]
[InlineData(301)]
[InlineData(302)]
[InlineData(303)]
[InlineData(304)]
[InlineData(307)]
[Theory]
public void GeneratesVoidReturnTypeForNoContent(int statusCode)
{
var myObjectSchema = new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"id", new OpenApiSchema {
Type = "string",
}
}
},
Reference = new OpenApiReference
{
Id = "myobject",
Type = ReferenceType.Schema
},
UnresolvedReference = false
};
var document = new OpenApiDocument
{
Paths = new OpenApiPaths
{
["answer"] = new OpenApiPathItem
{
Operations = {
[OperationType.Get] = new OpenApiOperation
{
Responses = new OpenApiResponses
{
[statusCode.ToString()] = new OpenApiResponse {
Content = {
["application/json"] = new OpenApiMediaType {
Schema = myObjectSchema
}
}
},
}
}
}
}
},
Components = new()
{
Schemas = new Dictionary<string, OpenApiSchema> {
{
"myobject", myObjectSchema
}
}
}
};
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration { ClientClassName = "TestClient", ClientNamespaceName = "TestSdk", ApiRootUrl = "https://localhost" }, _httpClient);
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
var rbNS = codeModel.FindNamespaceByName("TestSdk.Answer");
Assert.NotNull(rbNS);
var rbClass = rbNS.Classes.FirstOrDefault(x => x.IsOfKind(CodeClassKind.RequestBuilder));
Assert.NotNull(rbClass);
Assert.Single(rbClass.Methods.Where(x => x.IsOfKind(CodeMethodKind.RequestExecutor)));
var executor = rbClass.Methods.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.RequestExecutor));
Assert.NotNull(executor);
Assert.Equal("void", executor.ReturnType.Name);
}
[InlineData(new[] { "microsoft.graph.user", "microsoft.graph.termstore.term" }, "microsoft.graph")]
[InlineData(new[] { "microsoft.graph.user", "odata.errors.error" }, "")]
[InlineData(new string[] { }, "")]
Expand Down

0 comments on commit 8b65340

Please sign in to comment.