Skip to content

Commit

Permalink
Http payload media type (microsoft#4209)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash authored Aug 19, 2024
1 parent 261fa52 commit 197b709
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#Requires -Version 7.0
param(
$filter,
[bool]$Stubbed = $true
[bool]$Stubbed = $true,
[bool]$LaunchOnly = $false
)

Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;

$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')
$solutionDir = Join-Path $packageRoot 'generator'

Refresh-Build
if (-not $LaunchOnly) {
Refresh-Build

if ($null -eq $filter -or $filter -eq "Unbranded-TypeSpec") {
Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'
if ($null -eq $filter -or $filter -eq "Unbranded-TypeSpec") {
Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'

$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec"
$unbrandedTypespecTestProject = $unbrandedTypespecTestProject
$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec"
$unbrandedTypespecTestProject = $unbrandedTypespecTestProject

Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject)
Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject)

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan
Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj"
Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan
Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj"

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
}

Expand Down Expand Up @@ -59,7 +62,6 @@ $failingSpecs = @(
Join-Path 'http' 'parameters' 'spread'
Join-Path 'http' 'payload' 'content-negotiation'
Join-Path 'http' 'payload' 'json-merge-patch'
Join-Path 'http' 'payload' 'media-type'
Join-Path 'http' 'payload' 'multipart'
Join-Path 'http' 'payload' 'pageable'
Join-Path 'http' 'resiliency' 'srv-driven'
Expand Down Expand Up @@ -135,6 +137,9 @@ foreach ($directory in $directories) {
}

$cadlRanchLaunchProjects.Add(($folders -join "-"), ("TestProjects/CadlRanch/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))"))
if ($LaunchOnly) {
continue
}
Write-Host "Generating $subPath" -ForegroundColor Cyan
Invoke (Get-TspCommand $specFile $generationDir $stubbed)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ private IReadOnlyList<ValueExpression> GetParamConversions(IReadOnlyList<Paramet
{
conversions.Add(BinaryContentSnippets.Create(param));
}
else if (param.Location == ParameterLocation.Body && param.Type.Equals(typeof(string)))
{
var bdExpression = Operation.RequestBodyMediaType == BodyMediaType.Json
? BinaryDataSnippets.FromObjectAsJson(param)
: BinaryDataSnippets.FromString(param);
conversions.Add(BinaryContentSnippets.Create(bdExpression));
}
else
{
conversions.Add(param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-payload-media-type": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/payload/media-type -p StubLibraryPlugin",
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-payload-xml": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/payload/xml -p StubLibraryPlugin",
"commandName": "Executable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using NUnit.Framework;
using Payload.MediaType;

namespace TestProjects.CadlRanch.Tests.Http.Payload.MediaType
{
public class MediaTypeTests : CadlRanchTestBase
{
[CadlRanchTest]
public Task SendAsText() => Test(async (host) =>
{
var response1 = await new MediaTypeClient(host, null).GetStringBodyClient().SendAsTextAsync("{cat}");
Assert.AreEqual(200, response1.GetRawResponse().Status);
});

[CadlRanchTest]
public Task GetAsText() => Test(async (host) =>
{
var response2 = await new MediaTypeClient(host, null).GetStringBodyClient().GetAsTextAsync();
Assert.AreEqual("{cat}", response2.Value);
});

[CadlRanchTest]
[Ignore("https://github.com/microsoft/typespec/issues/4208")]
public Task SendAsJson() => Test(async (host) =>
{
var response3 = await new MediaTypeClient(host, null).GetStringBodyClient().SendAsJsonAsync("foo");
Assert.AreEqual(200, response3.GetRawResponse().Status);
});

[CadlRanchTest]
[Ignore("https://github.com/microsoft/typespec/issues/4208")]
public Task GetAsJson() => Test(async (host) =>
{
var response4 = await new MediaTypeClient(host, null).GetStringBodyClient().GetAsJsonAsync();
Assert.AreEqual("foo", response4.Value);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output-folder": ".",
"namespace": "Payload.MediaType",
"library-name": "Payload.MediaType",
"use-model-reader-writer": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Payload.MediaType", "src\Payload.MediaType.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;

namespace Payload.MediaType
{
public partial class MediaTypeClient
{
public MediaTypeClient() : this(new Uri("http://localhost:3000"), new MediaTypeClientOptions()) => throw null;

public MediaTypeClient(Uri endpoint, MediaTypeClientOptions options) => throw null;

public ClientPipeline Pipeline => throw null;

public virtual StringBody GetStringBodyClient() => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// <auto-generated/>

#nullable disable

using System.ClientModel.Primitives;

namespace Payload.MediaType
{
public partial class MediaTypeClientOptions : ClientPipelineOptions
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <auto-generated/>

#nullable disable

using System.ClientModel;
using System.ClientModel.Primitives;
using System.Threading.Tasks;

namespace Payload.MediaType
{
public partial class StringBody
{
protected StringBody() => throw null;

public ClientPipeline Pipeline => throw null;

public virtual ClientResult SendAsText(BinaryContent content, RequestOptions options) => throw null;

public virtual Task<ClientResult> SendAsTextAsync(BinaryContent content, RequestOptions options) => throw null;

public virtual ClientResult SendAsText(string text) => throw null;

public virtual Task<ClientResult> SendAsTextAsync(string text) => throw null;

public virtual ClientResult GetAsText(RequestOptions options) => throw null;

public virtual Task<ClientResult> GetAsTextAsync(RequestOptions options) => throw null;

public virtual ClientResult<string> GetAsText() => throw null;

public virtual Task<ClientResult<string>> GetAsTextAsync() => throw null;

public virtual ClientResult SendAsJson(BinaryContent content, RequestOptions options) => throw null;

public virtual Task<ClientResult> SendAsJsonAsync(BinaryContent content, RequestOptions options) => throw null;

public virtual ClientResult SendAsJson(string text) => throw null;

public virtual Task<ClientResult> SendAsJsonAsync(string text) => throw null;

public virtual ClientResult GetAsJson(RequestOptions options) => throw null;

public virtual Task<ClientResult> GetAsJsonAsync(RequestOptions options) => throw null;

public virtual ClientResult<string> GetAsJson() => throw null;

public virtual Task<ClientResult<string>> GetAsJsonAsync() => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>This is the Payload.MediaType client library for developing .NET applications with rich experience.</Description>
<AssemblyTitle>SDK Code Generation Payload.MediaType</AssemblyTitle>
<Version>1.0.0-beta.1</Version>
<PackageTags>Payload.MediaType</PackageTags>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ClientModel" Version="1.1.0-beta.4" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
Loading

0 comments on commit 197b709

Please sign in to comment.