Skip to content

Commit

Permalink
Merge pull request #4741 from microsoft/fix/scalar-types-discriminator
Browse files Browse the repository at this point in the history
Fix for scalar member composed types.
  • Loading branch information
SilasKenneth authored Jun 4, 2024
2 parents e1eee1b + 3deb2d8 commit a031b6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
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 @@ public class PhpRefiner : CommonLanguageRefiner
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 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
Name = "BaseRequestBuilder",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions",
Name = $@"{AbstractionsNamespaceName}",
IsExternal = true
}
}, AccessModifier.Public);
Expand All @@ -45,7 +46,10 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
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 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
AddParentClassToErrorClasses(
generatedCode,
"ApiException",
"Microsoft\\Kiota\\Abstractions"
AbstractionsNamespaceName
);
MoveClassesWithNamespaceNamesUnderNamespace(generatedCode);
AddConstructorsForDefaultValues(generatedCode, true);
Expand Down Expand Up @@ -106,7 +110,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
}
);
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 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
Name = "BaseRequestConfiguration",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions",
Name = AbstractionsNamespaceName,
IsExternal = true
}
});
Expand All @@ -143,7 +147,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
Name = "Date",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions\\Types",
Name = $@"{AbstractionsNamespaceName}\Types",
IsExternal = true,
},
})
Expand All @@ -154,7 +158,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
Name = "Time",
Declaration = new CodeType
{
Name = "Microsoft\\Kiota\\Abstractions\\Types",
Name = $@"{AbstractionsNamespaceName}\Types",
IsExternal = true,
},
})
Expand Down Expand Up @@ -184,34 +188,34 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
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 Down Expand Up @@ -339,7 +343,7 @@ private void AddCollectionValidationUtilImportToModels(CodeElement codeElement)
{
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 @@ -855,7 +855,7 @@ private void WriteModelFactoryMethodBody(CodeMethod codeElement, CodeClass paren
{
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 @@ private void WriteModelFactoryMethodBody(CodeMethod codeElement, CodeClass paren

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

0 comments on commit a031b6d

Please sign in to comment.