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

Multi-Value headers support for PHP Generation #2173

Merged
merged 7 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for external documentation links on request execution methods (PHP Generation). [2038](https://github.com/microsoft/kiota/issues/2138)
- Adds support for nullable reference types in dotnet for projects running Netstandard 2.1/Net 6.0 and above []()
- Added support for multi-value headers to CLI generation. (Shell)
- Add support for multi-value headers for PHP Generation. [#2052](https://github.com/microsoft/kiota/issues/2052)

### Changed

Expand Down
9 changes: 7 additions & 2 deletions src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
"Microsoft\\Kiota\\Abstractions", "RequestAdapter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
"Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation", "RequestOption"),
"Microsoft\\Kiota\\Abstractions", "HttpMethod", "RequestInformation"),
baywet marked this conversation as resolved.
Show resolved Hide resolved
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor),
"Microsoft\\Kiota\\Abstractions", "ResponseHandler"),
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 (static x => x is CodeProperty property && property.IsOfKind(CodePropertyKind.Headers),
"Microsoft\\Kiota\\Abstractions","RequestHeaders"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
"Microsoft\\Kiota\\Abstractions\\Serialization", "SerializationWriter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer),
Expand Down Expand Up @@ -188,10 +190,13 @@ private static void CorrectPropertyType(CodeProperty currentProperty) {
} else if (currentProperty.Type?.Name?.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase) ?? false)
{
currentProperty.Type.Name = "DateTime";
} else if (currentProperty.IsOfKind(CodePropertyKind.Options, CodePropertyKind.Headers))
} else if (currentProperty.IsOfKind(CodePropertyKind.Options))
{
currentProperty.Type.CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Complex;
currentProperty.Type.Name = "array";
} else if (currentProperty.IsOfKind(CodePropertyKind.Headers))
{
currentProperty.DefaultValue = $"new {currentProperty.Type?.Name}()";
}
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
}
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 @@ -372,7 +372,7 @@ private void WriteRequestConfiguration(RequestParams requestParams, LanguageWrit
var headersName = $"{requestConfigParamName}->{headers.Name.ToFirstCharacterLowerCase()}";
writer.WriteLine($"if ({headersName} !== null) {{");
writer.IncreaseIndent();
writer.WriteLine($"{RequestInfoVarName}->headers = array_merge({RequestInfoVarName}->headers, {headersName});");
writer.WriteLine($"{RequestInfoVarName}->headers->putAll({RequestInfoVarName}->headers);");
writer.CloseBlock();
}
if (queryString != null)
Expand All @@ -398,7 +398,7 @@ private void WriteRequestConfiguration(RequestParams requestParams, LanguageWrit
private void WriteAcceptHeaderDef(CodeMethod codeMethod, LanguageWriter writer)
{
if(codeMethod.AcceptedResponseTypes.Any())
writer.WriteLine($"{RequestInfoVarName}->headers = array_merge({RequestInfoVarName}->headers, [\"Accept\" => \"{string.Join(", ", codeMethod.AcceptedResponseTypes)}\"]);");
writer.WriteLine($"{RequestInfoVarName}->headers->add('Accept', \"{string.Join(", ", codeMethod.AcceptedResponseTypes)}\");");
}
private void WriteDeserializerBody(CodeClass parentClass, LanguageWriter writer, CodeMethod method) {
var inherits = parentClass.StartBlock?.Inherits != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void WriteRequestGeneratorForParsable()
result);
Assert.Contains("if ($requestConfiguration !== null", result);
Assert.Contains("if ($requestConfiguration->h !== null)", result);
Assert.Contains("$requestInfo->headers = array_merge($requestInfo->headers, $requestConfiguration->h);", result);
Assert.Contains("$requestInfo->headers->putAll($requestInfo->headers);", result);
Assert.Contains("$requestInfo->setQueryParameters($requestConfiguration->q);", result);
Assert.Contains("$requestInfo->addRequestOptions(...$requestConfiguration->o);", result);
Assert.Contains("$requestInfo->setContentFromParsable($this->requestAdapter, \"\", $body);", result);
Expand All @@ -415,7 +415,7 @@ public void WritesRequestGeneratorBodyForParsableCollection()
result);
Assert.Contains("if ($requestConfiguration !== null", result);
Assert.Contains("if ($requestConfiguration->h !== null)", result);
Assert.Contains("$requestInfo->headers = array_merge($requestInfo->headers, $requestConfiguration->h);", result);
Assert.Contains("$requestInfo->headers->putAll($requestInfo->headers);", result);
Assert.Contains("$requestInfo->setQueryParameters($requestConfiguration->q);", result);
Assert.Contains("$requestInfo->addRequestOptions(...$requestConfiguration->o);", result);
Assert.Contains("$requestInfo->setContentFromParsableCollection($this->requestAdapter, \"\", $body);", result);
Expand All @@ -441,7 +441,7 @@ public void WriteRequestGeneratorForScalarType()
result);
Assert.Contains("if ($requestConfiguration !== null", result);
Assert.Contains("if ($requestConfiguration->h !== null)", result);
Assert.Contains("$requestInfo->headers = array_merge($requestInfo->headers, $requestConfiguration->h);", result);
Assert.Contains("$requestInfo->headers->putAll($requestInfo->headers);", result);
Assert.Contains("$requestInfo->setQueryParameters($requestConfiguration->q);", result);
Assert.Contains("$requestInfo->addRequestOptions(...$requestConfiguration->o);", result);
Assert.Contains("$requestInfo->setContentFromScalar($this->requestAdapter, \"\", $body);", result);
Expand Down Expand Up @@ -472,7 +472,7 @@ public void WritesRequestGeneratorBodyForScalarCollection()
result);
Assert.Contains("if ($requestConfiguration !== null", result);
Assert.Contains("if ($requestConfiguration->h !== null)", result);
Assert.Contains("$requestInfo->headers = array_merge($requestInfo->headers, $requestConfiguration->h);", result);
Assert.Contains("$requestInfo->headers->putAll($requestInfo->headers);", result);
Assert.Contains("$requestInfo->setQueryParameters($requestConfiguration->q);", result);
Assert.Contains("$requestInfo->addRequestOptions(...$requestConfiguration->o);", result);
Assert.Contains("$requestInfo->setContentFromScalarCollection($this->requestAdapter, \"\", $body);", result);
Expand Down