Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Omondi committed Apr 8, 2024
1 parent 96da3af commit 039a84c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

### Changed

- Changed URI template generation to reuse templates when required templates are absent across operations.

## [1.13.0] - 2024-04-04

### Added
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 @@ -1290,7 +1290,7 @@ private void CreateOperationMethods(OpenApiUrlTreeNode currentNode, OperationTyp
};
var operationUrlTemplate = currentNode.GetUrlTemplate(operationType);
if (!operationUrlTemplate.Equals(parentClass.Properties.FirstOrDefault(static x => x.Kind is CodePropertyKind.UrlTemplate)?.DefaultValue?.Trim('"'), StringComparison.Ordinal)
&& currentNode.HasRequiredQueryParametersAcrossOperations())
&& currentNode.HasRequiredQueryParametersAcrossOperations())// no need to generate extra strings/templates as optional parameters will have no effect on resolved url.
generatorMethod.UrlTemplateOverride = operationUrlTemplate;

var mediaTypes = schema switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,93 @@ public void DifferentUrlTemplatesPerOperation()
}
});
var node = OpenApiUrlTreeNode.Create(doc, Label);
Assert.False(node.HasRequiredQueryParametersAcrossOperations());
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment{?%24select}", node.Children.First().Value.GetUrlTemplate());
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment{?%24select}", node.Children.First().Value.GetUrlTemplate(OperationType.Get));
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment", node.Children.First().Value.GetUrlTemplate(OperationType.Put));
// the query parameters will be decoded by a middleware at runtime before the request is executed
}
[Fact]
public void DifferentUrlTemplatesPerOperationWithRequiredParameter()
{
var doc = new OpenApiDocument
{
Paths = [],
};
doc.Paths.Add("{param-with-dashes}\\existing-segment", new()
{
Parameters = [
new()
{
Name = "param-with-dashes",
In = ParameterLocation.Path,
Required = true,
Schema = new()
{
Type = "string"
},
Style = ParameterStyle.Simple,
},
],
Operations = new Dictionary<OperationType, OpenApiOperation> {
{ OperationType.Get, new() {
Parameters = [

new (){
Name = "$select",
In = ParameterLocation.Query,
Schema = new () {
Type = "string"
},
Style = ParameterStyle.Simple,
}
]
}
},
{ OperationType.Post, new() {
Parameters = [

new (){
Name = "$expand",
In = ParameterLocation.Query,
Schema = new () {
Type = "string"
},
Style = ParameterStyle.Simple,
}
]
}
},
{
OperationType.Put, new() {}
},
{ OperationType.Delete, new() {
Parameters = [

new (){
Name = "id",
In = ParameterLocation.Query,
Schema = new () {
Type = "string"
},
Style = ParameterStyle.Simple,
Required = true
}
]
}
},
}
});
var node = OpenApiUrlTreeNode.Create(doc, Label);
Assert.True(node.HasRequiredQueryParametersAcrossOperations());
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment?id={id}{&%24expand,%24select}", node.Children.First().Value.GetUrlTemplate());//the default contains a combination of everything.
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment{?%24select}", node.Children.First().Value.GetUrlTemplate(OperationType.Get));
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment{?%24expand}", node.Children.First().Value.GetUrlTemplate(OperationType.Post));
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment", node.Children.First().Value.GetUrlTemplate(OperationType.Put));
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment?id={id}", node.Children.First().Value.GetUrlTemplate(OperationType.Delete));
// the query parameters will be decoded by a middleware at runtime before the request is executed
}
[Fact]
public void GeneratesRequiredQueryParametersAndOptionalMixInPathItem()
{
var doc = new OpenApiDocument
Expand Down

0 comments on commit 039a84c

Please sign in to comment.