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

Fix for scalar member composed types. #4741

Merged
merged 8 commits into from
Jun 4, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixes a bug where CLI Generation does not handle path parameters of type "string" and format "date", "date-time", "time", etc. [#4615](https://github.com/microsoft/kiota/issues/4615)
- Fixes a bug where request executors would be missing Untyped parameters in dotnet [#4692](https://github.com/microsoft/kiota/issues/4692)
- Fixes a bug where indexers in include/exclude patters were not normalized if the indexer was the last segment without a slash at the end [#4715](https://github.com/microsoft/kiota/issues/4715)
- Fixes a bug where CLI generation doesnot handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707)
- Fixes a bug where CLI generation doesn't handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707)
- Fixed a bug where models would not be created when a multipart content schema existed with no encoding [#4734](https://github.com/microsoft/kiota/issues/4734)
- Types generated by Kiota are now referenced with their full name to avoid namespace ambiguities [#4475](https://github.com/microsoft/kiota/issues/4475)
- Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761)
- Fix scalar member composed type serialization in PHP [#2827](https://github.com/microsoft/kiota/issues/2827)
- Trims unused components from output openApi document when generating plugins [#4672](https://github.com/microsoft/kiota/issues/4672)

## [1.14.0] - 2024-05-02
Expand Down
48 changes: 26 additions & 22 deletions src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
private static readonly CodeUsingDeclarationNameComparer usingComparer = new();
public PhpRefiner(GenerationConfiguration configuration) : base(configuration) { }

private const string AbstractionsNamespaceName = @"Microsoft\Kiota\Abstractions";

public override Task Refine(CodeNamespace generatedCode, CancellationToken cancellationToken)
{
Expand All @@ -31,7 +32,7 @@
Name = "BaseRequestBuilder",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions",
Name = $@"{AbstractionsNamespaceName}",
IsExternal = true
}
}, AccessModifier.Public);
Expand All @@ -45,7 +46,10 @@
ConvertUnionTypesToWrapper(generatedCode,
_configuration.UsesBackingStore,
static s => s,
false);
false,
$@"{AbstractionsNamespaceName}\Serialization",
"ComposedTypeWrapper"
);
ReplaceReservedNames(generatedCode, new PhpReservedNamesProvider(), reservedWord => $"Escaped{reservedWord.ToFirstCharacterUpperCase()}", new HashSet<Type> { typeof(CodeEnumOption) });
AddQueryParameterFactoryMethod(generatedCode);
AddPrimaryErrorMessage(generatedCode, "getPrimaryErrorMessage", () => new CodeType { IsExternal = true, IsNullable = false, Name = "string" });
Expand All @@ -71,7 +75,7 @@
AddParentClassToErrorClasses(
generatedCode,
"ApiException",
"Microsoft\\Kiota\\Abstractions"
AbstractionsNamespaceName
);
MoveClassesWithNamespaceNamesUnderNamespace(generatedCode);
AddConstructorsForDefaultValues(generatedCode, true);
Expand Down Expand Up @@ -106,7 +110,7 @@
}
);
cancellationToken.ThrowIfCancellationRequested();
AddSerializationModulesImport(generatedCode, ["Microsoft\\Kiota\\Abstractions\\ApiClientBuilder"], null, '\\');
AddSerializationModulesImport(generatedCode, [$@"{AbstractionsNamespaceName}\ApiClientBuilder"], null, '\\');
cancellationToken.ThrowIfCancellationRequested();
AddPropertiesAndMethodTypesImports(generatedCode, true, false, true);
CorrectBackingStoreSetterParam(generatedCode);
Expand All @@ -119,7 +123,7 @@
Name = "BaseRequestConfiguration",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions",
Name = AbstractionsNamespaceName,
IsExternal = true
}
});
Expand All @@ -143,7 +147,7 @@
Name = "Date",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions\\Types",
Name = $@"{AbstractionsNamespaceName}\Types",
IsExternal = true,
},
})
Expand All @@ -154,7 +158,7 @@
Name = "Time",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions\\Types",
Name = $@"{AbstractionsNamespaceName}\Types",
IsExternal = true,
},
})
Expand All @@ -171,7 +175,7 @@
})
},
{
"DateTimeOffset", ("DateTime", new CodeUsing

Check warning on line 178 in src/Kiota.Builder/Refiners/PhpRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'DateTime' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
{
Name = "DateTime",
Declaration = new CodeType
Expand All @@ -182,36 +186,36 @@
})
}
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 189 in src/Kiota.Builder/Refiners/PhpRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 18 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
"Microsoft\\Kiota\\Abstractions", "RequestAdapter"),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
"Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation"),
AbstractionsNamespaceName, "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"),
$@"{AbstractionsNamespaceName}\Serialization", "AdditionalDataHolder"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "SerializationWriter"),
$@"{AbstractionsNamespaceName}\Serialization", "SerializationWriter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNode"),
$@"{AbstractionsNamespaceName}\Serialization", "ParseNode"),
new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model),
"Microsoft\\Kiota\\Abstractions\\Serialization", "Parsable"),
$@"{AbstractionsNamespaceName}\Serialization", "Parsable"),
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"),
$@"{AbstractionsNamespaceName}\Store", "BackingStoreFactory", "BackingStoreFactorySingleton"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
"Microsoft\\Kiota\\Abstractions\\Store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ),
$@"{AbstractionsNamespaceName}\Store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ),
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 CodeEnum, AbstractionsNamespaceName, "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(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 CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor), AbstractionsNamespaceName, "ApiClientBuilder"),
new(static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.QueryParameter) && !string.IsNullOrEmpty(property.SerializationName), AbstractionsNamespaceName, "QueryParameter"),
new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), AbstractionsNamespaceName, "RequestOption"),
new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal),
"Microsoft\\Kiota\\Abstractions\\Serialization", "ParseNodeHelper"),
$@"{AbstractionsNamespaceName}\Serialization", "ParseNodeHelper"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) && method.Parameters.Any(static y => y.IsOfKind(CodeParameterKind.RequestBody) && y.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase)),
@"Microsoft\Kiota\Abstractions", MultipartBodyClassName)
AbstractionsNamespaceName, MultipartBodyClassName)
};

private const string MultipartBodyClassName = "MultiPartBody";
Expand All @@ -227,7 +231,7 @@
currentProperty.Type.Name = currentProperty.Type.Name[1..];
else if (currentProperty.IsOfKind(CodePropertyKind.AdditionalData))
{
currentProperty.Type.Name = "array";

Check warning on line 234 in src/Kiota.Builder/Refiners/PhpRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'array' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
currentProperty.Type.IsNullable = true;
currentProperty.DefaultValue = "[]";
currentProperty.Type.CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Complex;
Expand Down Expand Up @@ -339,7 +343,7 @@
{
if (codeElement is CodeClass codeClass && codeClass.Kind == CodeClassKind.Model)
{
var typeUtilsUsing = new CodeUsing { Name = "TypeUtils", Declaration = new CodeType { Name = "Microsoft\\Kiota\\Abstractions\\Types", IsExternal = true } };
var typeUtilsUsing = new CodeUsing { Name = "TypeUtils", Declaration = new CodeType { Name = $@"{AbstractionsNamespaceName}\Types", IsExternal = true } };
if (codeClass.Properties.Any(x =>
x.Kind == CodePropertyKind.Custom
&& x.Type is CodeType codeType
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
.ToArray();
foreach (var otherProp in otherProps)
{
writer.StartBlock($"{(includeElse ? "} else " : string.Empty)}if ($this->{otherProp.Getter!.Name.ToFirstCharacterLowerCase()}() !== null) {{");

Check warning on line 369 in src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal '} else ' 6 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
WriteSerializationMethodCall(otherProp, writer, "null");
writer.DecreaseIndent();
if (!includeElse)
Expand All @@ -386,7 +386,7 @@
.Select(static x => $"$this->{x.Getter!.Name.ToFirstCharacterLowerCase()}()")
.Order(StringComparer.OrdinalIgnoreCase)
.Aggregate(static (x, y) => $"{x}, {y}");
WriteSerializationMethodCall(complexProperties.First(), writer, "null", propertiesNames);

Check warning on line 389 in src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs

View workflow job for this annotation

GitHub Actions / Build

Indexing at 0 should be used instead of the "Enumerable" extension method "First" (https://rules.sonarsource.com/csharp/RSPEC-6608)
if (includeElse)
{
writer.CloseBlock();
Expand Down Expand Up @@ -855,7 +855,7 @@
{
if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType || parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
writer.WriteLine($"{ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();");
var writeDiscriminatorValueRead = parentClass.DiscriminatorInformation.ShouldWriteParseNodeCheck && !parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType;
var writeDiscriminatorValueRead = parentClass.DiscriminatorInformation is { ShouldWriteParseNodeCheck: true, ShouldWriteDiscriminatorForIntersectionType: false, HasBasicDiscriminatorInformation: true };

if (writeDiscriminatorValueRead &&
codeElement.Parameters.OfKind(CodeParameterKind.ParseNode) is CodeParameter parseNodeParameter)
Expand All @@ -868,7 +868,7 @@

if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForInheritedType)
WriteFactoryMethodBodyForInheritedModel(parentClass.DiscriminatorInformation.DiscriminatorMappings, writer, codeElement);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForUnionType && parentClass.DiscriminatorInformation.HasBasicDiscriminatorInformation)
else if (parentClass.DiscriminatorInformation is { ShouldWriteDiscriminatorForUnionType: true, HasBasicDiscriminatorInformation: true })
WriteFactoryMethodBodyForUnionModelForDiscriminatedTypes(codeElement, parentClass, writer);
else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType)
WriteFactoryMethodBodyForIntersectionModel(codeElement, parentClass, writer);
Expand Down
Loading