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

[Java] Move (most of) the name mangling logic to the Refiner #3265

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ private CodeClass AddModelClass(OpenApiUrlTreeNode currentNode, OpenApiSchema sc
// Add the class to the namespace after the serialization members
// as other threads looking for the existence of the class may find the class but the additional data/backing store properties may not be fully populated causing duplication
var includeAdditionalDataProperties = config.IncludeAdditionalData && schema.AdditionalPropertiesAllowed;
AddSerializationMembers(newClassStub, includeAdditionalDataProperties, config.UsesBackingStore);
AddSerializationMembers(newClassStub, includeAdditionalDataProperties, config.UsesBackingStore, static s => s);

var newClass = currentNamespace.AddClass(newClassStub).First();
var lifecycle = classLifecycles.GetOrAdd(currentNamespace.Name + "." + declarationName, static n => new());
Expand Down Expand Up @@ -1870,7 +1870,7 @@ private CodeClass AddModelClass(OpenApiUrlTreeNode currentNode, OpenApiSchema sc
type.TypeDefinition is CodeClass definition &&
definition.DerivesFrom(newClass)); // only the mappings that derive from the current class

AddDiscriminatorMethod(newClass, schema.GetDiscriminatorPropertyName(), mappings);
AddDiscriminatorMethod(newClass, schema.GetDiscriminatorPropertyName(), mappings, static s => s);
return newClass;
}
private IEnumerable<KeyValuePair<string, CodeTypeBase>> GetDiscriminatorMappings(OpenApiUrlTreeNode currentNode, OpenApiSchema schema, CodeNamespace currentNamespace, CodeClass? baseClass)
Expand Down Expand Up @@ -2012,11 +2012,11 @@ private void InitializeInheritanceIndex()
{
openApiDocument?.InitializeInheritanceIndex(inheritanceIndex);
}
internal static void AddDiscriminatorMethod(CodeClass newClass, string discriminatorPropertyName, IEnumerable<KeyValuePair<string, CodeTypeBase>> discriminatorMappings)
internal static void AddDiscriminatorMethod(CodeClass newClass, string discriminatorPropertyName, IEnumerable<KeyValuePair<string, CodeTypeBase>> discriminatorMappings, Func<string, string> refineMethodName)
{
var factoryMethod = new CodeMethod
{
Name = "CreateFromDiscriminatorValue",
Name = refineMethodName("CreateFromDiscriminatorValue"),
Documentation = new()
{
Description = "Creates a new instance of the appropriate class based on discriminator value",
Expand Down Expand Up @@ -2107,14 +2107,14 @@ private Dictionary<string, OpenApiSchema> CollectAllProperties(OpenApiSchema sch
internal const string BackedModelInterface = "IBackedModel";
private const string ParseNodeInterface = "IParseNode";
internal const string AdditionalHolderInterface = "IAdditionalDataHolder";
internal static void AddSerializationMembers(CodeClass model, bool includeAdditionalProperties, bool usesBackingStore)
internal static void AddSerializationMembers(CodeClass model, bool includeAdditionalProperties, bool usesBackingStore, Func<string, string> refineMethodName)
{
var serializationPropsType = $"IDictionary<string, Action<{ParseNodeInterface}>>";
if (!model.ContainsMember(FieldDeserializersMethodName))
{
var deserializeProp = new CodeMethod
{
Name = FieldDeserializersMethodName,
Name = refineMethodName(FieldDeserializersMethodName),
Kind = CodeMethodKind.Deserializer,
Access = AccessModifier.Public,
Documentation = new()
Expand All @@ -2136,7 +2136,7 @@ internal static void AddSerializationMembers(CodeClass model, bool includeAdditi
{
var serializeMethod = new CodeMethod
{
Name = SerializeMethodName,
Name = refineMethodName(SerializeMethodName),
Kind = CodeMethodKind.Serializer,
IsAsync = false,
Documentation = new()
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
MoveClassesWithNamespaceNamesUnderNamespace(generatedCode);
ConvertUnionTypesToWrapper(generatedCode,
_configuration.UsesBackingStore,
static s => s,
true,
AbstractionsNamespaceName,
"IComposedTypeWrapper"
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/CliRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();
MoveClassesWithNamespaceNamesUnderNamespace(generatedCode);
ConvertUnionTypesToWrapper(generatedCode,
_configuration.UsesBackingStore
_configuration.UsesBackingStore,
static s => s
);
AddPropertiesAndMethodTypesImports(generatedCode, false, false, false);
cancellationToken.ThrowIfCancellationRequested();
Expand Down
40 changes: 23 additions & 17 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,15 @@ current.Parent is CodeClass parentClass &&
{
var refinedName = refineAccessorName(currentProperty.Name);

if (string.IsNullOrEmpty(currentProperty.SerializationName) && !refinedName.Equals(currentProperty.Name, StringComparison.Ordinal))
currentProperty.SerializationName = currentProperty.Name;
if (!refinedName.Equals(currentProperty.Name, StringComparison.Ordinal) &&
!parentClass.Properties.Any(property => !currentProperty.Name.Equals(property.Name, StringComparison.Ordinal) &&
refinedName.Equals(property.Name, StringComparison.OrdinalIgnoreCase)))// ensure the refinement won't generate a duplicate
{
if (string.IsNullOrEmpty(currentProperty.SerializationName))
currentProperty.SerializationName = currentProperty.Name;

if (!parentClass.Properties.Any(property => refinedName.Equals(property.Name, StringComparison.OrdinalIgnoreCase)))// ensure the refinement won't generate a duplicate
parentClass.RenameChildElement(currentProperty.Name, refinedName);
}
}
CrawlTree(current, x => ReplacePropertyNames(x, propertyKindsToReplace!, refineAccessorName));
}
Expand Down Expand Up @@ -188,7 +192,7 @@ current.Parent is CodeClass parentClass &&
Deprecation = currentProperty.Deprecation,
}).First();
currentProperty.Getter.Name = $"{getterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix
var setter = parentClass.AddMethod(new CodeMethod
currentProperty.Setter = parentClass.AddMethod(new CodeMethod
{
Name = $"set-{accessorName}",
Access = AccessModifier.Public,
Expand All @@ -207,10 +211,9 @@ current.Parent is CodeClass parentClass &&
},
Deprecation = currentProperty.Deprecation,
}).First();
setter.Name = $"{setterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix
currentProperty.Setter = setter;
currentProperty.Setter.Name = $"{setterPrefix}{accessorName}"; // so we don't get an exception for duplicate names when no prefix

setter.AddParameter(new CodeParameter
currentProperty.Setter.AddParameter(new CodeParameter
{
Name = "value",
Kind = CodeParameterKind.SetterValue,
Expand Down Expand Up @@ -440,30 +443,33 @@ protected static void ReplaceBinaryByNativeType(CodeElement currentElement, stri
}
CrawlTree(currentElement, c => ReplaceBinaryByNativeType(c, symbol, ns, addDeclaration, isNullable));
}
protected static void ConvertUnionTypesToWrapper(CodeElement currentElement, bool usesBackingStore, bool supportInnerClasses = true, string markerInterfaceNamespace = "", string markerInterfaceName = "", string markerMethodName = "")
protected static void ConvertUnionTypesToWrapper(CodeElement currentElement, bool usesBackingStore, Func<string, string> refineMethodName, bool supportInnerClasses = true, string markerInterfaceNamespace = "", string markerInterfaceName = "", string markerMethodName = "")
{
ArgumentNullException.ThrowIfNull(currentElement);
ArgumentNullException.ThrowIfNull(refineMethodName);
if (currentElement.Parent is CodeClass parentClass)
{
if (currentElement is CodeMethod currentMethod)
{
currentMethod.Name = refineMethodName(currentMethod.Name);

if (currentMethod.ReturnType is CodeComposedTypeBase currentUnionType)
currentMethod.ReturnType = ConvertComposedTypeToWrapper(parentClass, currentUnionType, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
currentMethod.ReturnType = ConvertComposedTypeToWrapper(parentClass, currentUnionType, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
if (currentMethod.Parameters.Any(static x => x.Type is CodeComposedTypeBase))
foreach (var currentParameter in currentMethod.Parameters.Where(static x => x.Type is CodeComposedTypeBase))
currentParameter.Type = ConvertComposedTypeToWrapper(parentClass, (CodeComposedTypeBase)currentParameter.Type, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
currentParameter.Type = ConvertComposedTypeToWrapper(parentClass, (CodeComposedTypeBase)currentParameter.Type, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
if (currentMethod.ErrorMappings.Select(static x => x.Value).OfType<CodeComposedTypeBase>().Any())
foreach (var errorUnionType in currentMethod.ErrorMappings.Select(static x => x.Value).OfType<CodeComposedTypeBase>())
currentMethod.ReplaceErrorMapping(errorUnionType, ConvertComposedTypeToWrapper(parentClass, errorUnionType, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName));
currentMethod.ReplaceErrorMapping(errorUnionType, ConvertComposedTypeToWrapper(parentClass, errorUnionType, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName));
}
else if (currentElement is CodeIndexer currentIndexer && currentIndexer.ReturnType is CodeComposedTypeBase currentUnionType)
currentIndexer.ReturnType = ConvertComposedTypeToWrapper(parentClass, currentUnionType, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
currentIndexer.ReturnType = ConvertComposedTypeToWrapper(parentClass, currentUnionType, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
else if (currentElement is CodeProperty currentProperty && currentProperty.Type is CodeComposedTypeBase currentPropUnionType)
currentProperty.Type = ConvertComposedTypeToWrapper(parentClass, currentPropUnionType, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
currentProperty.Type = ConvertComposedTypeToWrapper(parentClass, currentPropUnionType, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName);
}
CrawlTree(currentElement, x => ConvertUnionTypesToWrapper(x, usesBackingStore, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName));
CrawlTree(currentElement, x => ConvertUnionTypesToWrapper(x, usesBackingStore, refineMethodName, supportInnerClasses, markerInterfaceNamespace, markerInterfaceName, markerMethodName));
}
private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, CodeComposedTypeBase codeComposedType, bool usesBackingStore, bool supportsInnerClasses, string markerInterfaceNamespace, string markerInterfaceName, string markerMethodName)
private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, CodeComposedTypeBase codeComposedType, bool usesBackingStore, Func<string, string> refineMethodName, bool supportsInnerClasses, string markerInterfaceNamespace, string markerInterfaceName, string markerMethodName)
{
ArgumentNullException.ThrowIfNull(codeClass);
ArgumentNullException.ThrowIfNull(codeComposedType);
Expand Down Expand Up @@ -528,7 +534,7 @@ private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, Co
if (codeComposedType.Types.All(static x => x.TypeDefinition is CodeClass targetClass && targetClass.IsOfKind(CodeClassKind.Model) ||
x.TypeDefinition is CodeEnum || x.TypeDefinition is null))
{
KiotaBuilder.AddSerializationMembers(newClass, false, usesBackingStore);
KiotaBuilder.AddSerializationMembers(newClass, false, usesBackingStore, refineMethodName);
newClass.Kind = CodeClassKind.Model;
}
newClass.OriginalComposedType = codeComposedType;
Expand Down Expand Up @@ -569,7 +575,7 @@ private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, Co
});
}
// Add the discriminator function to the wrapper as it will be referenced.
KiotaBuilder.AddDiscriminatorMethod(newClass, codeComposedType.DiscriminatorInformation.DiscriminatorPropertyName, codeComposedType.DiscriminatorInformation.DiscriminatorMappings);
KiotaBuilder.AddDiscriminatorMethod(newClass, codeComposedType.DiscriminatorInformation.DiscriminatorPropertyName, codeComposedType.DiscriminatorInformation.DiscriminatorMappings, refineMethodName);
return new CodeType
{
Name = newClass.Name,
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
ConvertUnionTypesToWrapper(
generatedCode,
_configuration.UsesBackingStore,
static s => s,
true,
string.Empty,
string.Empty,
Expand Down
42 changes: 39 additions & 3 deletions src/Kiota.Builder/Refiners/JavaRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
CorrectCommonNames(generatedCode);
MoveRequestBuilderPropertiesToBaseType(generatedCode,
new CodeUsing
{
Expand Down Expand Up @@ -52,13 +53,14 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
InsertOverrideMethodForRequestExecutorsAndBuildersAndConstructors(generatedCode);
ReplaceIndexersByMethodsWithParameter(generatedCode,
true,
static x => $"By{x.ToFirstCharacterUpperCase()}",
static x => $"by{x.ToFirstCharacterUpperCase()}",
static x => x.ToFirstCharacterLowerCase(),
GenerationLanguage.Java);
cancellationToken.ThrowIfCancellationRequested();
RemoveCancellationParameter(generatedCode);
ConvertUnionTypesToWrapper(generatedCode,
_configuration.UsesBackingStore,
s => s.ToFirstCharacterLowerCase(),
true,
SerializationNamespaceName,
"ComposedTypeWrapper"
Expand All @@ -69,7 +71,9 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
ReplacePropertyNames(generatedCode,
new() {
CodePropertyKind.Custom,
CodePropertyKind.AdditionalData,
CodePropertyKind.QueryParameter,
CodePropertyKind.RequestBuilder,
},
static s => s.ToCamelCase(UnderscoreArray).ToFirstCharacterLowerCase());
AddGetterAndSetterMethods(generatedCode,
Expand All @@ -78,7 +82,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
CodePropertyKind.AdditionalData,
CodePropertyKind.BackingStore,
},
static (_, s) => s.ToCamelCase(UnderscoreArray),
static (_, s) => s.ToCamelCase(UnderscoreArray).ToFirstCharacterUpperCase(),
_configuration.UsesBackingStore,
true,
"get",
Expand Down Expand Up @@ -137,7 +141,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
SplitLongDiscriminatorMethods(generatedCode);
AddPrimaryErrorMessage(generatedCode,
"getMessage",
() => new CodeType { Name = "string", IsNullable = false, IsExternal = true }
() => new CodeType { Name = "String", IsNullable = false, IsExternal = true }
);
}, cancellationToken);
}
Expand Down Expand Up @@ -250,6 +254,34 @@ private static void AddEnumSetImport(CodeElement currentElement)
AbstractionsNamespaceName, MultipartBodyClassName)
};
private const string MultipartBodyClassName = "MultipartBody";
private static void CorrectCommonNames(CodeElement currentElement)
{
if (currentElement is CodeMethod m &&
currentElement.Parent is CodeClass parentClass)
{
parentClass.RenameChildElement(m.Name, m.Name.ToFirstCharacterLowerCase());
}
else if (currentElement is CodeIndexer i)
{
i.IndexParameter.Name = i.IndexParameter.Name.ToFirstCharacterLowerCase();
}
else if (currentElement is CodeEnum e)
{
foreach (var option in e.Options)
{
if (!string.IsNullOrEmpty(option.Name) && Char.IsLower(option.Name[0]))
{
if (string.IsNullOrEmpty(option.SerializationName))
{
option.SerializationName = option.Name;
}
option.Name = option.Name.ToFirstCharacterUpperCase();
}
}
}

CrawlTree(currentElement, element => CorrectCommonNames(element));
}
private static void CorrectPropertyType(CodeProperty currentProperty)
{
if (currentProperty.IsOfKind(CodePropertyKind.RequestAdapter))
Expand Down Expand Up @@ -277,6 +309,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
if (!string.IsNullOrEmpty(currentProperty.DefaultValue))
currentProperty.DefaultValue = "new HashMap<>()";
}
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
}
private static void CorrectImplements(ProprietableBlockDeclaration block)
Expand Down Expand Up @@ -317,6 +350,9 @@ private static void CorrectMethodType(CodeMethod currentMethod)
.Select(static x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());

currentMethod.Parameters.ToList().ForEach(static x => x.Type.Name = x.Type.Name.ToFirstCharacterUpperCase());
currentMethod.ReturnType.Name = currentMethod.ReturnType.Name.ToFirstCharacterUpperCase();
}
private static readonly Dictionary<string, (string, CodeUsing?)> DateTypesReplacements = new(StringComparer.OrdinalIgnoreCase) {
{"DateTimeOffset", ("OffsetDateTime", new CodeUsing {
Expand Down
Loading
Loading