Skip to content

Commit 86b178b

Browse files
committed
test(cli): cover netstandard System.Net.Http.Json support (#26)
1 parent 21970fb commit 86b178b

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

src/tests/AutoSDK.IntegrationTests.Cli/CliSystemNetHttpJsonTests.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,77 @@ await GenerateFromContentAsync(
6666
});
6767
}
6868

69+
[TestMethod]
70+
public async Task Generate_WithSystemNetHttpJsonFlag_OnNetstandard20_WithPackage_Builds()
71+
{
72+
const string spec = """
73+
openapi: 3.0.3
74+
info:
75+
title: HttpJson
76+
version: 1.0.0
77+
paths:
78+
/people:
79+
post:
80+
operationId: createPerson
81+
requestBody:
82+
required: true
83+
content:
84+
application/json:
85+
schema:
86+
$ref: '#/components/schemas/Person'
87+
responses:
88+
'200':
89+
description: ok
90+
content:
91+
application/json:
92+
schema:
93+
$ref: '#/components/schemas/Person'
94+
/flag:
95+
get:
96+
operationId: getFlag
97+
responses:
98+
'200':
99+
description: ok
100+
content:
101+
application/json:
102+
schema:
103+
type: boolean
104+
components:
105+
schemas:
106+
Person:
107+
type: object
108+
required: [name]
109+
properties:
110+
name:
111+
type: string
112+
""";
113+
114+
await GenerateFromContentAsync(
115+
fileName: "system-net-http-json-netstandard20.yaml",
116+
specContent: spec,
117+
targetFramework: "netstandard2.0",
118+
packageReferences:
119+
[
120+
(PackageId: "System.Net.Http.Json", Version: "10.0.0"),
121+
],
122+
assertGeneratedOutput: async outputDirectory =>
123+
{
124+
var generatedContents = await Task.WhenAll(
125+
Directory.EnumerateFiles(outputDirectory, "*.g.cs", SearchOption.AllDirectories)
126+
.Select(path => File.ReadAllTextAsync(path)));
127+
var content = string.Join("\n\n", generatedContents);
128+
129+
content.Should().Contain("AutoSdkPolyfills.CreateJsonContent(");
130+
content.Should().Contain("global::System.Net.Http.Json.JsonContent.Create(");
131+
content.Should().Contain("global::System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync");
132+
});
133+
}
134+
69135
private static async Task GenerateFromContentAsync(
70136
string fileName,
71137
string specContent,
72138
string targetFramework = "net8.0",
139+
IReadOnlyCollection<(string PackageId, string Version)>? packageReferences = null,
73140
Func<string, Task>? assertGeneratedOutput = null)
74141
{
75142
var tempSpecDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
@@ -80,7 +147,7 @@ private static async Task GenerateFromContentAsync(
80147
try
81148
{
82149
await File.WriteAllTextAsync(specPath, specContent);
83-
await GenerateAsync(specPath, targetFramework, assertGeneratedOutput);
150+
await GenerateAsync(specPath, targetFramework, packageReferences, assertGeneratedOutput);
84151
}
85152
finally
86153
{
@@ -91,6 +158,7 @@ private static async Task GenerateFromContentAsync(
91158
private static async Task GenerateAsync(
92159
string specPath,
93160
string targetFramework,
161+
IReadOnlyCollection<(string PackageId, string Version)>? packageReferences,
94162
Func<string, Task>? assertGeneratedOutput)
95163
{
96164
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
@@ -122,6 +190,13 @@ private static async Task GenerateAsync(
122190
await assertGeneratedOutput(tempDirectory);
123191
}
124192

193+
var packageReferenceLines = packageReferences is { Count: > 0 }
194+
? string.Join(
195+
Environment.NewLine,
196+
packageReferences.Select(reference =>
197+
$@" <PackageReference Include=""{reference.PackageId}"" Version=""{reference.Version}"" />"))
198+
: string.Empty;
199+
125200
await File.WriteAllTextAsync(Path.Combine(tempDirectory, "Oag.csproj"), $@"<Project Sdk=""Microsoft.NET.Sdk"">
126201
127202
<PropertyGroup>
@@ -139,6 +214,13 @@ await File.WriteAllTextAsync(Path.Combine(tempDirectory, "Oag.csproj"), $@"<Proj
139214
<GenerateDocumentationFile>true</GenerateDocumentationFile>
140215
</PropertyGroup>
141216
217+
{(string.IsNullOrWhiteSpace(packageReferenceLines)
218+
? string.Empty
219+
: $@" <ItemGroup>
220+
{packageReferenceLines}
221+
</ItemGroup>
222+
")}
223+
142224
</Project>");
143225

144226
var buildResult = await RunDotnetAsync(

0 commit comments

Comments
 (0)