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

PHP - Add support for scalar types in request bodies #1937

Merged
merged 4 commits into from
Nov 7, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added support for scalar request bodies Python [#1571](https://github.com/microsoft/kiota/issues/1571)
- Added support for scalar request bodies in PHP [#1937](https://github.com/microsoft/kiota/pull/1937)

### Changed

- Fixed a bug where array buffers nullability would wrongly be defined for TypeScript.
- Fixed a bug where parameter comments would apprear in summary tag comments in dotnet. [#1945](https://github.com/microsoft/kiota/issues/1945)
- Fixed a bug in PHP generation where request bodies would not serialize single elements properly. [#1937](https://github.com/microsoft/kiota/pull/1937)

## [0.7.1] - 2022-11-01

Expand Down
31 changes: 18 additions & 13 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private void WriteRequestBuilderWithParametersBody(string returnType, LanguageWr
private static string GetPropertyCall(CodeProperty property, string defaultValue) => property == null ? defaultValue : $"$this->{property.Name}";
private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer)
{
if(codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
var requestInformationClass = "RequestInformation";
var pathParametersProperty = currentClass.GetPropertyOfKind(CodePropertyKind.PathParameters);
var urlTemplateProperty = currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate);
Expand All @@ -341,6 +341,22 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
$"{RequestInfoVarName}->pathParameters = {GetPropertyCall(pathParametersProperty, "''")};",
$"{RequestInfoVarName}->httpMethod = HttpMethod::{codeElement?.HttpMethod?.ToString().ToUpperInvariant()};");
WriteAcceptHeaderDef(codeElement, writer);
WriteRequestConfiguration(requestParams, writer);
if (requestParams.requestBody != null) {
var suffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty;
if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase))
writer.WriteLine($"{RequestInfoVarName}->setStreamContent({conventions.GetParameterName(requestParams.requestBody)});");
if (requestParams.requestBody.Type is CodeType bodyType && bodyType.TypeDefinition is CodeClass) {
writer.WriteLine($"{RequestInfoVarName}->setContentFromParsable{suffix}($this->{requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {conventions.GetParameterName(requestParams.requestBody)});");
}
writer.WriteLine($"{RequestInfoVarName}->setContentFromScalar{suffix}($this->{requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {conventions.GetParameterName(requestParams.requestBody)});");
}

writer.WriteLine($"return {RequestInfoVarName};");
}

private void WriteRequestConfiguration(RequestParams requestParams, LanguageWriter writer)
{
if (requestParams.requestConfiguration != null)
{
var queryString = requestParams.QueryParameters;
Expand All @@ -349,7 +365,7 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
var requestConfigParamName = conventions.GetParameterName(requestParams.requestConfiguration);
writer.WriteLine($"if ({requestConfigParamName} !== null) {{");
writer.IncreaseIndent();
if(headers != null) {
if (headers != null) {
var headersName = $"{requestConfigParamName}->{headers.Name.ToFirstCharacterLowerCase()}";
writer.WriteLine($"if ({headersName} !== null) {{");
writer.IncreaseIndent();
Expand All @@ -374,17 +390,6 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
}
writer.CloseBlock();
}

if(requestParams.requestBody != null) {
if(requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase))
writer.WriteLine($"{RequestInfoVarName}->setStreamContent({conventions.GetParameterName(requestParams.requestBody)});");
else {
var spreadOperator = requestParams.requestBody.Type.AllTypes.First().IsCollection ? "..." : string.Empty;
writer.WriteLine($"{RequestInfoVarName}->setContentFromParsable($this->{requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {spreadOperator}{conventions.GetParameterName(requestParams.requestBody)});");
}
}

writer.WriteLine($"return {RequestInfoVarName};");
}

private void WriteAcceptHeaderDef(CodeMethod codeMethod, LanguageWriter writer)
Expand Down
Loading