Skip to content

Commit

Permalink
Merge pull request #4593 from microsoft/feat/multipart-body-form-seri…
Browse files Browse the repository at this point in the history
…alization

Feat/multipart body form serialization
  • Loading branch information
SilasKenneth authored May 6, 2024
2 parents 6ac3cf6 + 77f0ef9 commit 448ce97
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added support for multipart form data request body in PHP. [#3029](https://github.com/microsoft/kiota/issues/3029)
- Added uri-form encoded serialization for PHP. [#2074](https://github.com/microsoft/kiota/issues/2074)

### Changed

- Aligns naming of sliced OpenAPI description generated by `plugin add` should be named `<plugin-name>-openapi.json|yml`
Expand Down
22 changes: 11 additions & 11 deletions README.md

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions it/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@
"Language": "ruby",
"Rationale": "https://github.com/microsoft/kiota/issues/2484"
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/3029"
},
{
"Language": "python",
"Rationale": "https://github.com/microsoft/kiota/issues/2957"
Expand Down Expand Up @@ -258,10 +254,6 @@
"Language": "go",
"Rationale": "https://github.com/microsoft/kiota/issues/2834"
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/3029"
},
{
"Language": "java",
"Rationale": "https://github.com/microsoft/kiota/issues/2842"
Expand Down Expand Up @@ -315,10 +307,6 @@
{
"Language": "python",
"Rationale": "https://github.com/microsoft/kiota/issues/3030"
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/3029"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions it/php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"microsoft/kiota-serialization-text": "*",
"microsoft/kiota-http-guzzle": "*",
"microsoft/kiota-serialization-json": "*",
"microsoft/kiota-serialization-form": "*",
"microsoft/kiota-serialization-multipart": "*",
"microsoft/kiota-authentication-phpleague": "*"
},
"require-dev": {
Expand Down
20 changes: 18 additions & 2 deletions src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
defaultConfiguration.Serializers,
new(StringComparer.OrdinalIgnoreCase) {
"Microsoft\\Kiota\\Serialization\\Json\\JsonSerializationWriterFactory",
"Microsoft\\Kiota\\Serialization\\Text\\TextSerializationWriterFactory"}
"Microsoft\\Kiota\\Serialization\\Text\\TextSerializationWriterFactory",
@"Microsoft\Kiota\Serialization\Form\FormSerializationWriterFactory",
@"Microsoft\Kiota\Serialization\Multipart\MultipartSerializationWriterFactory"
}
);
ReplaceDefaultDeserializationModules(generatedCode,
defaultConfiguration.Deserializers,
new(StringComparer.OrdinalIgnoreCase) {
"Microsoft\\Kiota\\Serialization\\Json\\JsonParseNodeFactory",
"Microsoft\\Kiota\\Serialization\\Text\\TextParseNodeFactory"}
"Microsoft\\Kiota\\Serialization\\Text\\TextParseNodeFactory",
@"Microsoft\Kiota\Serialization\Form\FormParseNodeFactory"
}
);
cancellationToken.ThrowIfCancellationRequested();
AddSerializationModulesImport(generatedCode, ["Microsoft\\Kiota\\Abstractions\\ApiClientBuilder"], null, '\\');
Expand Down Expand Up @@ -204,7 +210,12 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
new(static x => x is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestConfiguration), "Microsoft\\Kiota\\Abstractions", "RequestOption"),
new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal),
"Microsoft\\Kiota\\Abstractions\\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)
};

private const string MultipartBodyClassName = "MultiPartBody";

private static void CorrectPropertyType(CodeProperty currentProperty)
{
if (currentProperty.IsOfKind(CodePropertyKind.RequestAdapter))
Expand Down Expand Up @@ -276,6 +287,11 @@ private static void CorrectParameterType(CodeElement codeElement)
x.Type.Name = "BackingStoreFactory";
x.DefaultValue = "null";
});
currentMethod.Parameters.Where(x => x.Type.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase))
.ToList().ForEach(static y =>
{
y.Type.Name = "MultiPartBody";
});
}
CrawlTree(codeElement, CorrectParameterType);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/PhpReservedNamesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public class PhpReservedNamesProvider : IReservedNamesProvider
"xor",
"yield",
"yield from",
"BaseRequestBuilder"
"BaseRequestBuilder",
"MultipartBody"
});

public HashSet<string> ReservedNames => _reservedNames.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
writer.WriteLine($"{RequestInfoVarName}->setStreamContent({conventions.GetParameterName(requestParams.requestBody)}, \"{sanitizedRequestBodyContentType}\");");
}
else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty)
if (requestParams.requestBody.Type is CodeType bodyType && bodyType.TypeDefinition is CodeClass)
if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultiPartBody", StringComparison.OrdinalIgnoreCase)))
writer.WriteLine($"{RequestInfoVarName}->setContentFromParsable{suffix}($this->{requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{sanitizedRequestBodyContentType}\", {conventions.GetParameterName(requestParams.requestBody)});");
else
writer.WriteLine($"{RequestInfoVarName}->setContentFromScalar{suffix}($this->{requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{sanitizedRequestBodyContentType}\", {conventions.GetParameterName(requestParams.requestBody)});");
Expand Down
8 changes: 8 additions & 0 deletions src/kiota/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@
{
"Name": "microsoft/kiota-serialization-text",
"Version": "1.0.1"
},
{
"Name": "microsoft/kiota-serialization-multipart",
"Version": "1.0.0"
},
{
"Name": "microsoft/kiota-serialization-form",
"Version": "1.0.0"
}
],
"DependencyInstallCommand": "composer require {0}:{1}"
Expand Down
20 changes: 20 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2508,4 +2508,24 @@ public void WritesRequestGeneratorContentTypeQuotes()
var result = stringWriter.ToString();
Assert.Contains("\"application/json; profile=\\\"CamelCase\\\"\"", result);
}
[Fact]
public async Task WritesRequestGeneratorBodyForMultipart()
{
setup();
method.Kind = CodeMethodKind.RequestGenerator;
method.HttpMethod = HttpMethod.Post;
AddRequestProperties();
AddRequestBodyParameters();
method.Parameters.OfKind(CodeParameterKind.RequestBody)!.Type = new CodeType
{
Name = "MultipartBody",
IsExternal = true
};
method.RequestBodyContentType = "multipart/form-data";
await _refiner.Refine(root, new CancellationToken(false));
languageWriter.Write(method);
var result = stringWriter.ToString();
Assert.Contains("MultiPartBody $body", result);
Assert.Contains("$requestInfo->setContentFromParsable($this->requestAdapter, \"multipart/form-data\", $body);", result);
}
}

0 comments on commit 448ce97

Please sign in to comment.