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

Add Url Encoded Body support #1042

Merged
42 commits merged into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
67782d6
Add Url Encoded Body support
chamons Feb 18, 2021
d926777
Remove builder
chamons Feb 18, 2021
74b7297
Name tuple with record
chamons Feb 18, 2021
3e3e2ab
Prefer properties
chamons Feb 18, 2021
408b162
Correct generator for tostring of enums
chamons Feb 18, 2021
71d6f83
Remove unnecessary ctor
chamons Feb 19, 2021
8a8c603
Merge branch 'feature/v3' of https://github.com/Azure/autorest.csharp…
chamons Feb 19, 2021
72ab1b1
Revert part of json config
chamons Feb 19, 2021
893de85
Update to most recent autotest (will fail on CI until lands
chamons Feb 19, 2021
4bbf332
Remove explicit Build on FormUrlEncodedContent
chamons Feb 19, 2021
f3a5517
Update src/AutoRest.CSharp/Generation/Writers/RestClientWriter.cs
chamons Feb 19, 2021
87f8ff1
Use variable declaration instead of hard coded
chamons Feb 19, 2021
cfe4ed2
Don't hardcode request
chamons Feb 19, 2021
e387e83
Merge branch '986' of https://github.com/chamons/autorest.csharp into…
chamons Feb 19, 2021
7376e0d
Bump testserver
chamons Feb 19, 2021
7543d0e
Update src/AutoRest.CSharp/Generation/Writers/RestClientWriter.cs
chamons Feb 19, 2021
933d614
Update src/AutoRest.CSharp/Generation/Writers/RestClientWriter.cs
chamons Feb 19, 2021
b8b3a4d
Bump autorest core
chamons Feb 19, 2021
477f162
Merge branch '986' of https://github.com/chamons/autorest.csharp into…
chamons Feb 19, 2021
c32a7a4
Remove record usage
chamons Feb 19, 2021
8ca2167
Update to M4 dev build
chamons Feb 19, 2021
cc36875
Bump to release
chamons Feb 19, 2021
a611d98
Update autorest-core version
chamons Feb 22, 2021
554292f
Apply more coffee to last change
chamons Feb 22, 2021
2b78bbc
Merge branch 'feature/v3' of https://github.com/Azure/autorest.csharp…
chamons Feb 22, 2021
2ea6f86
Fix autorest core bump
chamons Feb 22, 2021
e7f16c0
Bump to autorest core w\ fix
chamons Feb 23, 2021
51d403d
Code generation
chamons Feb 23, 2021
1f702f9
Bump autorest core again
chamons Feb 24, 2021
8f328ca
Disable smoke test that requires swagger fix
chamons Feb 24, 2021
15257bb
Disable a second test for the same reason
chamons Feb 24, 2021
313ce93
One more disable
chamons Mar 1, 2021
64b1064
Merge branch 'feature/v3' into 986
chamons Mar 1, 2021
8220328
Fix ordering
chamons Mar 1, 2021
a572a6d
Another test bites the dust
chamons Mar 1, 2021
f514b34
Regenerate
chamons Mar 1, 2021
ecfef01
Reset even harder
chamons Mar 1, 2021
8b824fe
Revert "Fix ordering"
chamons Mar 1, 2021
e92acdb
Trigger Build
chamons Mar 1, 2021
fa71fcf
Nuke DefinesAllScenarios test
chamons Mar 1, 2021
b424b7e
Remove 2nd test param
chamons Mar 1, 2021
9a4dba6
Remove vscode bits
chamons Mar 1, 2021
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 eng/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ $testNames =
'body-duration',
'body-file',
'body-formdata',
'body-formdata-urlencoded',
'body-integer',
'body-number',
'body-string',
Expand Down
336 changes: 168 additions & 168 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"description": "package.json intended for in-repo use only, package.json used for publishing is located in src/AutoRest.CSharp/package.json",
"devDependencies": {
"@microsoft.azure/autorest.testserver": "3.0.8",
"autorest": "3.0.6335"
"@microsoft.azure/autorest.testserver": "3.0.19",
"autorest": "3.0.6339"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ input-file: "swagger-document"

```yaml
# autorest-core version
version: 3.0.6335
version: 3.1.0-dev.2
save-inputs: true
use: $(this-folder)/artifacts/bin/AutoRest.CSharp/Debug/netcoreapp3.0/
clear-output-folder: true
Expand Down
27 changes: 27 additions & 0 deletions src/AutoRest.CSharp/Generation/Writers/RestClientWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Response = Azure.Response;

namespace AutoRest.CSharp.Generation.Writers
Expand Down Expand Up @@ -243,6 +244,23 @@ private void WriteRequestCreation(CodeWriter writer, RestClientMethod clientMeth
flattenedSchemaRequestBody.Serialization,
w => w.Append(modelVariable));
break;
case UrlEncodedBody urlEncodedRequestBody:
var urlContent = new CodeWriterDeclaration("content");

WriteHeaders(writer, clientMethod, request, content: true);
writer.Line($"var {urlContent:D} = new {typeof(FormUrlEncodedContent)}();");

foreach (var (name, value) in urlEncodedRequestBody.Values)
{
using (WriteValueNullCheck(writer, value))
{
writer.Append($"{urlContent}.Add({name:L},");
WriteConstantOrParameterAsString(writer, value);
writer.Line($");");
}
}
chamons marked this conversation as resolved.
Show resolved Hide resolved
writer.Line($"{request}.Content = {urlContent};");
break;
case null:
break;
default:
Expand Down Expand Up @@ -363,6 +381,15 @@ private void WriteOperation(CodeWriter writer, RestClientMethod operation, bool
writer.Line();
}

private void WriteConstantOrParameterAsString(CodeWriter writer, ReferenceOrConstant constantOrReference)
{
WriteConstantOrParameter(writer, constantOrReference, enumAsString: true);
if (constantOrReference.Type.IsFrameworkType && constantOrReference.Type.FrameworkType != typeof(string))
{
writer.Append($".ToString()");
}
}

private void WriteConstantOrParameter(CodeWriter writer, ReferenceOrConstant constantOrReference, bool ignoreNullability = false, bool enumAsString = false)
{
if (constantOrReference.IsConstant)
Expand Down
64 changes: 34 additions & 30 deletions src/AutoRest.CSharp/Input/Generated/CodeModel.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ObjectSerialization BuildObject(KnownMediaType mediaType, ObjectSchema ob
case KnownMediaType.Xml:
return BuildXmlObjectSerialization(objectSchema, type);
default:
throw new NotImplementedException(mediaType.ToString());
throw new NotImplementedException($"BuildObject with {mediaType} from {objectSchema.Name}");
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/AutoRest.CSharp/Output/Models/Requests/UrlEncodedBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace AutoRest.CSharp.Output.Models.Requests
{
internal class UrlEncodedBody : RequestBody
{
public struct NamedReferenceOrConstant
{
public string Name { get; }
public ReferenceOrConstant Value { get; }

public NamedReferenceOrConstant (string name, ReferenceOrConstant value)
{
Name = name;
Value = value;
}

public void Deconstruct (out string name, out ReferenceOrConstant value)
{
name = Name;
value = Value;
}
}

public List<NamedReferenceOrConstant> Values { get; set; }= new List<NamedReferenceOrConstant>();

public void Add (string parameter, ReferenceOrConstant value)
{
Values.Add(new NamedReferenceOrConstant(parameter, value));
}
}
}
18 changes: 16 additions & 2 deletions src/AutoRest.CSharp/Output/Models/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ private Dictionary<ServiceRequest, RestClientMethod> EnsureGetNextPageMethods()
return _nextPageMethods;
}

private string GetRequestParameterName (RequestParameter requestParameter)
{
string defaultName = requestParameter.Language.Default.Name;
return requestParameter.Language.Default.SerializedName ?? defaultName;
}

private RestClientMethod BuildMethod(Operation operation, HttpRequest httpRequest, ICollection<RequestParameter> requestParameters, ResponseHeaderGroupType? responseHeaderModel)
{
HttpWithBodyRequest? httpRequestWithBody = httpRequest as HttpWithBodyRequest;
Expand All @@ -173,8 +179,7 @@ private RestClientMethod BuildMethod(Operation operation, HttpRequest httpReques
RequestParameter[] parameters = operation.Parameters.Concat(requestParameters).ToArray();
foreach (RequestParameter requestParameter in parameters)
{
string defaultName = requestParameter.Language.Default.Name;
string serializedName = requestParameter.Language.Default.SerializedName ?? defaultName;
string serializedName = GetRequestParameterName(requestParameter);
ReferenceOrConstant constantOrReference;
Schema valueSchema = requestParameter.Schema;

Expand Down Expand Up @@ -286,6 +291,15 @@ private RestClientMethod BuildMethod(Operation operation, HttpRequest httpReques
}
body = new MultipartRequestBody(value.ToArray());
}
else if (httpRequestWithBody.KnownMediaType == KnownMediaType.Form)
{
UrlEncodedBody urlbody = new UrlEncodedBody();
foreach (var (bodyRequestParameter, bodyParameterValue) in bodyParameters)
{
urlbody.Add(GetRequestParameterName(bodyRequestParameter), bodyParameterValue);
}
body = urlbody;
}
else
{
Debug.Assert(bodyParameters.Count == 1);
Expand Down
4 changes: 4 additions & 0 deletions src/AutoRest.CSharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestServerProjects\\body-formdata\\Generated"
},
"body-formdata-urlencoded": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestServerProjects\\body-formdata-urlencoded\\Generated"
},
"body-integer": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestServerProjects\\body-integer\\Generated"
Expand Down
7 changes: 7 additions & 0 deletions src/AutoRest.CSharp/Utilities/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace System.Runtime.CompilerServices
{
internal class IsExternalInit{}
}
2 changes: 1 addition & 1 deletion src/AutoRest.CSharp/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Configuration
```yaml
use-extension:
"@autorest/modelerfour": "4.15.456"
"@autorest/modelerfour": "4.17.0"
modelerfour:
always-create-content-type-parameter: true
flatten-models: true
Expand Down
2 changes: 1 addition & 1 deletion src/AutoRest.CodeModel/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class Program
private static void Main()
{
using var webClient = new WebClient();
webClient.DownloadFile(@"https://raw.githubusercontent.com/Azure/perks/master/codemodel/.resources/all-in-one/json/code-model.json", "code-model.json");
webClient.DownloadFile(@"https://raw.githubusercontent.com/Azure/autorest/master/packages/libs/codemodel/.resources/all-in-one/json/code-model.json", "code-model.json");

var schemaJson = File.ReadAllText("code-model.json")
// Makes Choices only have string values
Expand Down
42 changes: 23 additions & 19 deletions src/AutoRest.CodeModel/code-model.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ApiVersion": {
"description": "- since API version formats range from \nAzure ARM API date style (2018-01-01) to semver (1.2.3) \nand virtually any other text, this value tends to be an \nopaque string with the possibility of a modifier to indicate\nthat it is a range.\n\noptions: \n- prepend a dash or append a plus to indicate a range \n(ie, '2018-01-01+' or '-2019-01-01', or '1.0+' )\n\n- semver-range style (ie, '^1.0.0' or '~1.0.0' )",
"description": "- since API version formats range from\nAzure ARM API date style (2018-01-01) to semver (1.2.3)\nand virtually any other text, this value tends to be an\nopaque string with the possibility of a modifier to indicate\nthat it is a range.\n\noptions:\n- prepend a dash or append a plus to indicate a range\n(ie, '2018-01-01+' or '-2019-01-01', or '1.0+' )\n\n- semver-range style (ie, '^1.0.0' or '~1.0.0' )",
"type": "object",
"properties": {
"version": {
Expand Down Expand Up @@ -1041,6 +1041,10 @@
"groupedBy": {
"$ref": "#/definitions/Parameter",
"description": "When a parameter is grouped into another, this will tell where the parameter got grouped into."
},
"isPartialBody": {
"description": "If this parameter is only part of the body request(for multipart and form bodies.)",
"type": "boolean"
}
},
"defaultProperties": [],
Expand Down Expand Up @@ -1802,7 +1806,7 @@
}
},
"choices": {
"description": "- this is essentially can be thought of as an 'enum' \nthat is a choice between one of several items, but an unspecified value is permitted.",
"description": "- this is essentially can be thought of as an 'enum'\nthat is a choice between one of several items, but an unspecified value is permitted.",
"type": "array",
"items": {
"$ref": "#/definitions/ChoiceSchema"
Expand Down Expand Up @@ -1860,7 +1864,7 @@
}
},
"unknowns": {
"description": "it's possible that we just may make this an error \nin representation.",
"description": "it's possible that we just may make this an error\nin representation.",
"type": "array",
"items": {
"$ref": "#/definitions/Schema"
Expand Down Expand Up @@ -2214,6 +2218,20 @@
"defaultProperties": [],
"additionalProperties": false
},
"HttpMethod": {
"description": "standard HTTP protocol methods",
"enum": [
"delete",
"get",
"head",
"options",
"patch",
"post",
"put",
"trace"
],
"type": "string"
},
"SerializationStyle": {
"description": "The Serialization Style used for the parameter.\n\nDescribes how the parameter value will be serialized depending on the type of the parameter value.",
"enum": [
Expand Down Expand Up @@ -2248,20 +2266,6 @@
],
"type": "string"
},
"HttpMethod": {
"description": "standard HTTP protocol methods",
"enum": [
"delete",
"get",
"head",
"options",
"patch",
"post",
"put",
"trace"
],
"type": "string"
},
"ParameterLocation": {
"description": "the location that this parameter is placed in the http request",
"enum": [
Expand Down Expand Up @@ -2716,7 +2720,7 @@
"type": "object",
"properties": {
"path": {
"description": "A relative path to an individual endpoint. \n\nThe field name MUST begin with a slash. \nThe path is appended (no relative URL resolution) to the expanded URL from the Server Object's url field in order to construct the full URL. \nPath templating is allowed. \n\nWhen matching URLs, concrete (non-templated) paths would be matched before their templated counterparts.",
"description": "A relative path to an individual endpoint.\n\nThe field name MUST begin with a slash.\nThe path is appended (no relative URL resolution) to the expanded URL from the Server Object's url field in order to construct the full URL.\nPath templating is allowed.\n\nWhen matching URLs, concrete (non-templated) paths would be matched before their templated counterparts.",
"type": "string"
},
"uri": {
Expand Down Expand Up @@ -2801,7 +2805,7 @@
"type": "object",
"properties": {
"multipart": {
"description": "indicates that the HTTP Request should be a multipart request \n\nie, that it has multiple requests in a single request.",
"description": "indicates that the HTTP Request should be a multipart request\n\nie, that it has multiple requests in a single request.",
"type": "boolean",
"enum": [
true
Expand Down
Loading