Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/typescript escaped usings #3798

Merged
merged 13 commits into from
Nov 27, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Groups request builders and inline request/response bodies in the same file in TypeScript.
- Fixed a bug where reserved name rename would not rename objects properly. [#3609](https://github.com/microsoft/kiota/issues/3609)
- Switched to a Jammy Chiseled base image for docker containers.
- Fixed a bug where path parameters deduplication would create collisions on sub path segments. [#3757](https://github.com/microsoft/kiota/issues/3757)
- Moved from net7 to net8.
Expand Down
9 changes: 0 additions & 9 deletions src/Kiota.Builder/CodeDOM/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,6 @@ public IEnumerable<T> FindChildrenByName<T>(string childName) where T : ICodeEle

return Enumerable.Empty<T>();
}

public IEnumerable<T?> FindChildrenByName<T>(IEnumerable<string> childrenName, bool findInChildElements = true) where T : ICodeElement
{
ArgumentNullException.ThrowIfNull(childrenName);

return childrenName.Where(static x => !string.IsNullOrEmpty(x))
.Select(x => FindChildByName<T>(x, findInChildElements));
}

public T? FindChildByName<T>(string childName, bool findInChildElements = true) where T : ICodeElement
{
ArgumentException.ThrowIfNullOrEmpty(childName);
Expand Down
30 changes: 14 additions & 16 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
protected static void AddGetterAndSetterMethods(CodeElement current, HashSet<CodePropertyKind> propertyKindsToAddAccessors, Func<CodeElement, string, string> refineAccessorName, bool removeProperty, bool parameterAsOptional, string getterPrefix, string setterPrefix, string fieldPrefix = "_", AccessModifier propertyAccessModifier = AccessModifier.Private)
{
ArgumentNullException.ThrowIfNull(refineAccessorName);
var isSetterPrefixEmpty = string.IsNullOrEmpty(setterPrefix);
var isGetterPrefixEmpty = string.IsNullOrEmpty(getterPrefix);
if (propertyKindsToAddAccessors is null || propertyKindsToAddAccessors.Count == 0) return;
if (current is CodeProperty currentProperty &&
!currentProperty.ExistsInBaseType &&
Expand All @@ -179,7 +181,7 @@

currentProperty.Getter = parentClass.AddMethod(new CodeMethod
{
Name = $"get-{accessorName}",
Name = $"{(isGetterPrefixEmpty ? "get-" : getterPrefix)}{accessorName}",
Access = AccessModifier.Public,
IsAsync = false,
Kind = CodeMethodKind.Getter,
Expand All @@ -191,10 +193,11 @@
AccessedProperty = currentProperty,
Deprecation = currentProperty.Deprecation,
}).First();
currentProperty.Getter.Name = $"{getterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix
if (isGetterPrefixEmpty)
currentProperty.Getter.Name = $"{getterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix
currentProperty.Setter = parentClass.AddMethod(new CodeMethod
{
Name = $"set-{accessorName}",
Name = $"{(isSetterPrefixEmpty ? "set-" : setterPrefix)}{accessorName}",
Access = AccessModifier.Public,
IsAsync = false,
Kind = CodeMethodKind.Setter,
Expand All @@ -211,7 +214,8 @@
},
Deprecation = currentProperty.Deprecation,
}).First();
currentProperty.Setter.Name = $"{setterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix
if (isSetterPrefixEmpty)
currentProperty.Setter.Name = $"{setterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix

currentProperty.Setter.AddParameter(new CodeParameter
{
Expand Down Expand Up @@ -306,8 +310,7 @@
// in the CodeNamespace if-block so we also need to update the using references
if (!codeElementExceptions?.Contains(typeof(CodeNamespace)) ?? true)
ReplaceReservedCodeUsingNamespaceSegmentNames(currentDeclaration, provider, replacement);
if (currentDeclaration.Inherits?.Name is string inheritName && provider.ReservedNames.Contains(inheritName) && (currentDeclaration.Inherits is not CodeType))
currentDeclaration.Inherits.Name = replacement(currentDeclaration.Inherits.Name);
// we don't need to rename the inheritance name as it's either external and shouldn't change or it's generated and the code type maps directly to the source
}
else if (current is CodeNamespace currentNamespace &&
isNotInExceptions &&
Expand All @@ -316,18 +319,13 @@
ReplaceReservedNamespaceSegments(currentNamespace, provider, replacement);
else if (current is CodeMethod currentMethod &&
isNotInExceptions &&
shouldReplace)
shouldReplace &&
provider.ReservedNames.Contains(currentMethod.Name) &&
current.Parent is IBlock parentBlock)
{
if (provider.ReservedNames.Contains(currentMethod.Name))
currentMethod.Name = replacement.Invoke(currentMethod.Name);
parentBlock.RenameChildElement(current.Name, replacement.Invoke(currentMethod.Name));
}
else if (current is CodeProperty currentProperty &&
isNotInExceptions &&
shouldReplace &&
currentProperty.Type is CodeType propertyType &&
!propertyType.IsExternal &&
provider.ReservedNames.Contains(currentProperty.Type.Name))
propertyType.Name = replacement.Invoke(propertyType.Name);
// we don't need to property type name as it's either external and shouldn't change or it's generated and the code type maps directly to the source
andrueastman marked this conversation as resolved.
Show resolved Hide resolved

// Check if the current name meets the following conditions to be replaced
// 1. In the list of reserved names
Expand Down Expand Up @@ -611,9 +609,9 @@
if (currentElement is CodeIndexer currentIndexer &&
currentElement.Parent is CodeClass indexerParentClass)
{
if (indexerParentClass.ContainsMember(currentElement.Name)) // TODO remove condition for v2 necessary because of the second case of Go block

Check warning on line 612 in src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
indexerParentClass.RemoveChildElement(currentElement);
//TODO remove whole block except for last else if body for v2

Check warning on line 614 in src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
if (language == GenerationLanguage.Go)
{
if (currentIndexer.IsLegacyIndexer)
Expand Down
26 changes: 13 additions & 13 deletions src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,31 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
}
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
"Microsoft\\Kiota\\Abstractions", "RequestAdapter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
"Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation"),
new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model) && @class.Properties.Any(static y => y.IsOfKind(CodePropertyKind.AdditionalData)),
"Microsoft\\Kiota\\Abstractions\\Serialization", "AdditionalDataHolder"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "SerializationWriter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNode"),
new (x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model),
new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model),
"Microsoft\\Kiota\\Abstractions\\Serialization", "Parsable"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) &&
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) &&
method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)),
"Microsoft\\Kiota\\Abstractions\\Store", "BackingStoreFactory", "BackingStoreFactorySingleton"),
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
"Microsoft\\Kiota\\Abstractions\\Store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "Http\\Promise", "Promise"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "", "Exception"),
new (x => x is CodeEnum, "Microsoft\\Kiota\\Abstractions\\", "Enum"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "Http\\Promise", "Promise"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor), "", "Exception"),
new (static x => x is CodeEnum, "Microsoft\\Kiota\\Abstractions\\", "Enum"),
new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTime", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"),
new(static x => x is CodeProperty {Type.Name: {}} property && property.Type.Name.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase), "", "\\DateTime"),
new(x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), "Microsoft\\Kiota\\Abstractions", "ApiClientBuilder"),
new(x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), "Microsoft\\Kiota\\Abstractions", "QueryParameter"),
new(x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), "Microsoft\\Kiota\\Abstractions", "RequestOption"),
new(static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), "Microsoft\\Kiota\\Abstractions", "ApiClientBuilder"),
new(static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), "Microsoft\\Kiota\\Abstractions", "QueryParameter"),
new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), "Microsoft\\Kiota\\Abstractions", "RequestOption"),
new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal),
"Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNodeHelper"),
};
Expand Down
12 changes: 6 additions & 6 deletions src/Kiota.Builder/Refiners/RubyRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,24 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, CorrectImplements);
cancellationToken.ThrowIfCancellationRequested();
ReplacePropertyNames(generatedCode,
new() {
[
CodePropertyKind.Custom,
CodePropertyKind.QueryParameter,
},
],
static s => s.ToSnakeCase());
AddParentClassToErrorClasses(
generatedCode,
"ApiError",
"MicrosoftKiotaAbstractions",
true
);
ReplaceReservedNames(generatedCode, reservedNamesProvider, x => $"{x}_escaped");
AddGetterAndSetterMethods(generatedCode,
new() {
[
CodePropertyKind.Custom,
CodePropertyKind.AdditionalData,
CodePropertyKind.BackingStore,
},
],
static (_, s) => s.ToSnakeCase(),
_configuration.UsesBackingStore,
true,
Expand All @@ -92,9 +93,8 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
generatedCode,
true,
false,
new[] { CodeClassKind.RequestConfiguration });
[CodeClassKind.RequestConfiguration]);
ShortenLongNamespaceNames(generatedCode);
ReplaceReservedNames(generatedCode, reservedNamesProvider, x => $"{x}_escaped");
if (generatedCode.FindNamespaceByName(_configuration.ClientNamespaceName)?.Parent is CodeNamespace parentOfClientNS)
AddNamespaceModuleImports(parentOfClientNS, generatedCode);
var defaultConfiguration = new GenerationConfiguration();
Expand Down
Loading
Loading