Skip to content

Commit f59c13d

Browse files
authored
Allow multi patch http content type header (#521)
* header from obj * update git version mod * remove json patch test * test for json patch
1 parent e18ec35 commit f59c13d

File tree

8 files changed

+673
-539
lines changed

8 files changed

+673
-539
lines changed

.github/workflows/buildtest.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
os: [ubuntu-latest, windows-latest, macOS-latest]
99
name: Dotnet build
1010
steps:
11-
- uses: actions/checkout@v1
11+
- uses: actions/checkout@v2
1212
with:
1313
fetch-depth: 0
1414
- name: Setup dotnet SDK 2.1
@@ -19,6 +19,10 @@ jobs:
1919
uses: actions/setup-dotnet@v1
2020
with:
2121
dotnet-version: '3.1.x'
22+
- name: Setup dotnet SDK 5
23+
uses: actions/setup-dotnet@v1
24+
with:
25+
dotnet-version: '5.0.x'
2226
- name: Setup Format
2327
run: dotnet tool install -g dotnet-format
2428
- name: Check Format
@@ -48,6 +52,10 @@ jobs:
4852
uses: actions/setup-dotnet@v1
4953
with:
5054
dotnet-version: '3.1.x'
55+
- name: Setup dotnet SDK 5
56+
uses: actions/setup-dotnet@v1
57+
with:
58+
dotnet-version: '5.0.x'
5159
- name: Minikube
5260
run: minikube start
5361
- name: Test
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Net.Http.Headers;
3+
using k8s.Models;
4+
5+
namespace k8s
6+
{
7+
public partial class Kubernetes
8+
{
9+
public virtual MediaTypeHeaderValue GetHeader(object body)
10+
{
11+
if (body == null)
12+
{
13+
throw new ArgumentNullException(nameof(body));
14+
}
15+
16+
return MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
17+
}
18+
19+
20+
public virtual MediaTypeHeaderValue GetHeader(V1Patch body)
21+
{
22+
if (body == null)
23+
{
24+
throw new ArgumentNullException(nameof(body));
25+
}
26+
27+
switch (body.Type)
28+
{
29+
case V1Patch.PatchType.JsonPatch:
30+
return MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");
31+
case V1Patch.PatchType.MergePatch:
32+
return MediaTypeHeaderValue.Parse("application/merge-patch+json; charset=utf-8");
33+
case V1Patch.PatchType.StrategicMergePatch:
34+
return MediaTypeHeaderValue.Parse("application/strategic-merge-patch+json; charset=utf-8");
35+
default:
36+
throw new ArgumentOutOfRangeException(nameof(body.Type), "");
37+
}
38+
}
39+
}
40+
}

src/KubernetesClient/KubernetesClient.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Authors>The Kubernetes Project Authors</Authors>
44
<Copyright>2017 The Kubernetes Project Authors</Copyright>
@@ -30,18 +30,19 @@
3030
<PackageReference Include="AutoMapper" Version="7.0.1" Condition="'$(TargetFramework)' == 'net452'" />
3131
<PackageReference Include="AutoMapper" Version="9.0.0" Condition="'$(TargetFramework)' != 'net452'" />
3232
<PackageReference Include="Fractions" Version="4.0.1" />
33-
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="1.1.2" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.1'" />
34-
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.1'" />
35-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.0.50" PrivateAssets="all" />
33+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.3.37" PrivateAssets="all" />
3634
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
3735
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.10" />
3836
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.1'" />
3937
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.1'" />
38+
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" Condition="'$(TargetFramework)' == 'net452'" />
4039
<PackageReference Include="YamlDotNet" Version="8.1.2" />
4140
<PackageReference Include="System.Buffers" Version="4.5.1" Condition="'$(TargetFramework)' != 'netcoreapp2.1'" />
4241
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4342
</ItemGroup>
4443
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
44+
<Reference Include="System.Runtime.InteropServices" />
45+
<Reference Include="System.Net.Http" />
4546
<Reference Include="System.Net.Http.WebRequest" />
4647
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
4748
</ItemGroup>

src/KubernetesClient/V1Patch.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Microsoft.AspNetCore.JsonPatch;
32
using Newtonsoft.Json;
43

54
namespace k8s.Models
@@ -9,38 +8,32 @@ public partial class V1Patch
98
{
109
public enum PatchType
1110
{
11+
Unknown,
1212
JsonPatch,
1313
MergePatch,
1414
StrategicMergePatch,
1515
}
1616

1717
public PatchType Type { get; private set; }
1818

19-
public V1Patch(IJsonPatchDocument jsonPatch)
20-
: this((object)jsonPatch)
19+
public V1Patch(object body, PatchType type)
2120
{
22-
}
23-
24-
public V1Patch(String body, PatchType type)
25-
: this(body)
26-
{
27-
this.Type = type;
21+
Content = body;
22+
Type = type;
23+
CustomInit();
2824
}
2925

3026
partial void CustomInit()
3127
{
32-
if (Content is IJsonPatchDocument)
28+
if (Content == null)
3329
{
34-
Type = PatchType.JsonPatch;
35-
return;
30+
throw new ArgumentNullException(nameof(Content), "object must be set");
3631
}
3732

38-
if (Content is String)
33+
if (Type == PatchType.Unknown)
3934
{
40-
return;
35+
throw new ArgumentException("patch type must be set", nameof(Type));
4136
}
42-
43-
throw new NotSupportedException();
4437
}
4538
}
4639
}

0 commit comments

Comments
 (0)