Skip to content

Commit 37fe281

Browse files
authored
Merge pull request #2922 from whyfate/improve-restapi-accept-header
Remove the Charset from the Accept Header and replace it with the Accept-Charset Header.
2 parents f257ab2 + c75aab7 commit 37fe281

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

src/Hl7.Fhir.Base/CompatibilitySuppressions.xml

+28
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,32 @@
77
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
88
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
99
</Suppression>
10+
<Suppression>
11+
<DiagnosticId>CP0002</DiagnosticId>
12+
<Target>M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
13+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
14+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
15+
<IsBaselineSuppression>true</IsBaselineSuppression>
16+
</Suppression>
17+
<Suppression>
18+
<DiagnosticId>CP0002</DiagnosticId>
19+
<Target>M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
20+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
21+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
22+
<IsBaselineSuppression>true</IsBaselineSuppression>
23+
</Suppression>
24+
<Suppression>
25+
<DiagnosticId>CP0002</DiagnosticId>
26+
<Target>M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
27+
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
28+
<Right>lib/netstandard2.0/Hl7.Fhir.Base.dll</Right>
29+
<IsBaselineSuppression>true</IsBaselineSuppression>
30+
</Suppression>
31+
<Suppression>
32+
<DiagnosticId>CP0002</DiagnosticId>
33+
<Target>M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
34+
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
35+
<Right>lib/netstandard2.0/Hl7.Fhir.Base.dll</Right>
36+
<IsBaselineSuppression>true</IsBaselineSuppression>
37+
</Suppression>
1038
</Suppressions>

src/Hl7.Fhir.Base/Rest/ContentType.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,19 @@ public static ResourceFormat GetResourceFormatFromContentType(string? contentTyp
102102
/// </summary>
103103
/// <param name="format">Whether the body is xml or json.</param>
104104
/// <param name="fhirVersion">Optional. The version of FHIR to add to the header.</param>
105-
public static string BuildContentType(ResourceFormat format, string? fhirVersion = default) =>
106-
BuildMediaType(format, fhirVersion).ToString();
105+
/// <param name="excludeCharset">Optional. Whether exclude charset.</param>
106+
public static string BuildContentType(ResourceFormat format, string? fhirVersion = default, bool excludeCharset = false) =>
107+
BuildMediaType(format, fhirVersion, excludeCharset).ToString();
107108

108109
/// <summary>
109110
/// Creates a <see cref="MediaTypeHeaderValue"/> for use in a Content-Type header,
110111
/// given the serialization format and the fhir version in use.
111112
/// </summary>
112113
/// <param name="format">Whether the body is xml or json.</param>
113114
/// <param name="fhirVersion">Optional. The version of FHIR to add to the header.</param>
115+
/// <param name="excludeCharset">Optional. Whether exclude charset.</param>
114116
/// <exception cref="ArgumentException">Unsupported serialization.</exception>
115-
public static MediaTypeHeaderValue BuildMediaType(ResourceFormat format, string? fhirVersion = default)
117+
public static MediaTypeHeaderValue BuildMediaType(ResourceFormat format, string? fhirVersion = default, bool excludeCharset = false)
116118
{
117119
var contentType = format switch
118120
{
@@ -121,10 +123,11 @@ public static MediaTypeHeaderValue BuildMediaType(ResourceFormat format, string?
121123
_ => throw new ArgumentException("Cannot determine content type for data format " + format),
122124
};
123125

124-
var result = new MediaTypeHeaderValue(contentType)
126+
var result = new MediaTypeHeaderValue(contentType);
127+
if (!excludeCharset)
125128
{
126-
CharSet = Encoding.UTF8.WebName
127-
};
129+
result.CharSet = Encoding.UTF8.WebName;
130+
}
128131

129132
if (fhirVersion is not null && SemVersion.TryParse(fhirVersion, out var version))
130133
{

src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ public static HttpRequestMessage WithAccept(this HttpRequestMessage message,
134134
string? contentTypeFhirVersion,
135135
bool requestCompressedResponse)
136136
{
137-
message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(
138-
ContentType.BuildContentType(serialization, contentTypeFhirVersion)));
137+
message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(ContentType.BuildContentType(serialization, contentTypeFhirVersion, true)));
138+
message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue(Encoding.UTF8.WebName));
139139

140140
if (requestCompressedResponse)
141141
{

src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public void TestBuildingContentType(ResourceFormat format, string fhirVersion, s
4545
ContentType.BuildContentType(format, fhirVersion).Should().Be(expected);
4646
}
4747

48+
[DataTestMethod]
49+
[DataRow(ResourceFormat.Json, "", true, "application/fhir+json")]
50+
[DataRow(ResourceFormat.Xml, "", true, "application/fhir+xml")]
51+
public void TestBuildingContentTypeWithExcludeCharSet(ResourceFormat format, string fhirVersion, bool excludeCharset, string expected)
52+
{
53+
ContentType.BuildContentType(format, fhirVersion, excludeCharset).Should().Be(expected);
54+
}
55+
4856
[TestMethod]
4957
public void GetResourceFormatSupportsCharset()
5058
{

src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ public void SetAccept(ResourceFormat fmt)
204204
{
205205
var settings = new FhirClientSettings { PreferredFormat = fmt, BinaryReceivePreference = BinaryTransferBehaviour.UseResource };
206206
var request = makeMessage(settings: settings, method: Bundle.HTTPVerb.POST);
207-
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION));
207+
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION, true));
208208
request.Headers.AcceptEncoding.Should().BeEmpty();
209209

210210
settings.PreferCompressedResponses = true;
211211
request = makeMessage(settings: settings, method: Bundle.HTTPVerb.POST);
212-
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION));
212+
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION, true));
213213
request.Headers.AcceptEncoding.Select(h => h.Value).Should().BeEquivalentTo("gzip", "deflate");
214214
}
215215

0 commit comments

Comments
 (0)