Skip to content

Commit

Permalink
- adds support for indexer parameter description
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Aug 4, 2023
1 parent 61a370b commit c7ecd1d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added support for indexer parameter description. [#2978](https://github.com/microsoft/kiota/issues/2978)

### Changed

## [1.5.0] - 2023-08-04
Expand Down
25 changes: 15 additions & 10 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ private IEnumerable<CodeType> GetUnmappedTypeDefinitions(CodeElement codeElement
};
}
private static CodeType DefaultIndexerParameterType => new() { Name = "string", IsExternal = true };
private CodeType GetIndexerParameterType(OpenApiUrlTreeNode currentNode, OpenApiUrlTreeNode parentNode)
private CodeParameter GetIndexerParameterType(OpenApiUrlTreeNode currentNode, OpenApiUrlTreeNode parentNode)
{
var parameterName = currentNode.Path[parentNode.Path.Length..].Trim('\\', ForwardSlash, '{', '}');
var parameter = currentNode.PathItems.TryGetValue(Constants.DefaultOpenApiLabel, out var pathItem) ? pathItem.Parameters
Expand All @@ -987,9 +987,19 @@ private CodeType GetIndexerParameterType(OpenApiUrlTreeNode currentNode, OpenApi
_ => GetPrimitiveType(parameter.Schema),
} ?? DefaultIndexerParameterType;
type.IsNullable = false;
return type;
var result = new CodeParameter
{
Type = type,
SerializationName = currentNode.Segment.SanitizeParameterNameForUrlTemplate(),
Name = currentNode.Segment.CleanupSymbolName(),
Documentation = new()
{
Description = parameter?.Description.CleanupDescription() is string description && !string.IsNullOrEmpty(description) ? description : "Unique identifier of the item",
},
};
return result;
}
private CodeIndexer[] CreateIndexer(string childIdentifier, string childType, CodeType parameterType, OpenApiUrlTreeNode currentNode, OpenApiUrlTreeNode parentNode)
private CodeIndexer[] CreateIndexer(string childIdentifier, string childType, CodeParameter parameter, OpenApiUrlTreeNode currentNode, OpenApiUrlTreeNode parentNode)
{
logger.LogTrace("Creating indexer {Name}", childIdentifier);
var result = new List<CodeIndexer> { new CodeIndexer
Expand All @@ -1002,15 +1012,10 @@ private CodeIndexer[] CreateIndexer(string childIdentifier, string childType, Co
ReturnType = new CodeType { Name = childType },
PathSegment = parentNode.GetNodeNamespaceFromPath(string.Empty).Split('.').Last(),
Deprecation = currentNode.GetDeprecationInformation(),
IndexParameter = new() {
Type = parameterType,
SerializationName = currentNode.Segment.SanitizeParameterNameForUrlTemplate(),
Name = currentNode.Segment.CleanupSymbolName(),
//TODO description
}
IndexParameter = parameter,
}};

if (!"string".Equals(parameterType.Name, StringComparison.OrdinalIgnoreCase))
if (!"string".Equals(parameter.Type.Name, StringComparison.OrdinalIgnoreCase))
{ // adding a second indexer for the string version of the parameter so we keep backward compatibility
//TODO remove for v2

Check warning on line 1020 in src/Kiota.Builder/KiotaBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment.
var backCompatibleValue = (CodeIndexer)result[0].Clone();
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Writers/CSharp/CodeIndexerWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override void WriteCodeElement(CodeIndexer codeElement, LanguageWriter wr
if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("The parent of a property should be a class");
var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement);
conventions.WriteShortDescription(codeElement.Documentation.Description, writer);
writer.WriteLine($"{conventions.DocCommentPrefix}<param name=\"position\">{codeElement.IndexParameter.Documentation.Description.CleanupXMLString()}</param>");
conventions.WriteDeprecationAttribute(codeElement, writer);
writer.StartBlock($"public {returnType} this[{conventions.GetTypeString(codeElement.IndexParameter.Type, codeElement)} position] {{ get {{");
if (parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProp)
Expand Down
5 changes: 4 additions & 1 deletion tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5711,7 +5711,8 @@ public async Task IndexerAndRequestBuilderNamesMatch()
var collectionRequestBuilder = collectionRequestBuilderNamespace.FindChildByName<CodeClass>("postsRequestBuilder");
var collectionIndexer = collectionRequestBuilder.Indexer;
Assert.NotNull(collectionIndexer);
Assert.Equal("string", collectionIndexer.IndexParameter.Type.Name);
Assert.Equal("string", collectionIndexer.IndexParameter.Type.Name, StringComparer.OrdinalIgnoreCase);
Assert.Equal("Unique identifier of the item", collectionIndexer.IndexParameter.Documentation.Description, StringComparer.OrdinalIgnoreCase);
Assert.False(collectionIndexer.Deprecation.IsDeprecated);
var itemRequestBuilderNamespace = codeModel.FindNamespaceByName("ApiSdk.me.posts.item");
Assert.NotNull(itemRequestBuilderNamespace);
Expand All @@ -5735,6 +5736,7 @@ public async Task IndexerTypeIsAccurateAndBackwardCompatibleIndexersAreAdded()
- name: post-id
in: path
required: true
description: The id of the pet to retrieve
schema:
type: integer
format: int32
Expand Down Expand Up @@ -5765,6 +5767,7 @@ public async Task IndexerTypeIsAccurateAndBackwardCompatibleIndexersAreAdded()
var collectionIndexer = collectionRequestBuilder.Indexer;
Assert.NotNull(collectionIndexer);
Assert.Equal("integer", collectionIndexer.IndexParameter.Type.Name);
Assert.Equal("The id of the pet to retrieve", collectionIndexer.IndexParameter.Documentation.Description, StringComparer.OrdinalIgnoreCase);
Assert.False(collectionIndexer.IndexParameter.Type.IsNullable);
Assert.False(collectionIndexer.Deprecation.IsDeprecated);
var collectionStringIndexer = collectionRequestBuilder.FindChildByName<CodeIndexer>($"{collectionIndexer.Name}-string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public CodeIndexerWriterTests()
Name = "string",
},
SerializationName = "id",
Documentation = new()
{
Description = "some description"
}
}
};
parentClass.AddIndexer(indexer);
Expand Down Expand Up @@ -75,6 +79,7 @@ public void WritesIndexer()
Assert.Contains("RequestAdapter", result);
Assert.Contains("PathParameters", result);
Assert.Contains("id\", position", result);
Assert.Contains("some description", result);
Assert.Contains("public SomeRequestBuilder this[string position]", result);
AssertExtensions.CurlyBracesAreClosed(result);
}
Expand Down

0 comments on commit c7ecd1d

Please sign in to comment.