Skip to content

Commit

Permalink
Merge pull request #2922 from whyfate/improve-restapi-accept-header
Browse files Browse the repository at this point in the history
Remove the Charset from the Accept Header and replace it with the Accept-Charset Header.
  • Loading branch information
ewoutkramer authored Oct 18, 2024
2 parents f257ab2 + c75aab7 commit 37fe281
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
28 changes: 28 additions & 0 deletions src/Hl7.Fhir.Base/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,32 @@
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String)</Target>
<Left>lib/netstandard2.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/netstandard2.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
15 changes: 9 additions & 6 deletions src/Hl7.Fhir.Base/Rest/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ public static ResourceFormat GetResourceFormatFromContentType(string? contentTyp
/// </summary>
/// <param name="format">Whether the body is xml or json.</param>
/// <param name="fhirVersion">Optional. The version of FHIR to add to the header.</param>
public static string BuildContentType(ResourceFormat format, string? fhirVersion = default) =>
BuildMediaType(format, fhirVersion).ToString();
/// <param name="excludeCharset">Optional. Whether exclude charset.</param>
public static string BuildContentType(ResourceFormat format, string? fhirVersion = default, bool excludeCharset = false) =>
BuildMediaType(format, fhirVersion, excludeCharset).ToString();

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

var result = new MediaTypeHeaderValue(contentType)
var result = new MediaTypeHeaderValue(contentType);
if (!excludeCharset)
{
CharSet = Encoding.UTF8.WebName
};
result.CharSet = Encoding.UTF8.WebName;
}

if (fhirVersion is not null && SemVersion.TryParse(fhirVersion, out var version))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public static HttpRequestMessage WithAccept(this HttpRequestMessage message,
string? contentTypeFhirVersion,
bool requestCompressedResponse)
{
message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(
ContentType.BuildContentType(serialization, contentTypeFhirVersion)));
message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(ContentType.BuildContentType(serialization, contentTypeFhirVersion, true)));
message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue(Encoding.UTF8.WebName));

if (requestCompressedResponse)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void TestBuildingContentType(ResourceFormat format, string fhirVersion, s
ContentType.BuildContentType(format, fhirVersion).Should().Be(expected);
}

[DataTestMethod]
[DataRow(ResourceFormat.Json, "", true, "application/fhir+json")]
[DataRow(ResourceFormat.Xml, "", true, "application/fhir+xml")]
public void TestBuildingContentTypeWithExcludeCharSet(ResourceFormat format, string fhirVersion, bool excludeCharset, string expected)
{
ContentType.BuildContentType(format, fhirVersion, excludeCharset).Should().Be(expected);
}

[TestMethod]
public void GetResourceFormatSupportsCharset()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ public void SetAccept(ResourceFormat fmt)
{
var settings = new FhirClientSettings { PreferredFormat = fmt, BinaryReceivePreference = BinaryTransferBehaviour.UseResource };
var request = makeMessage(settings: settings, method: Bundle.HTTPVerb.POST);
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION));
request.Headers.Accept.Single().ToString().Should().Be(ContentType.BuildContentType(fmt, TESTVERSION, true));
request.Headers.AcceptEncoding.Should().BeEmpty();

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

Expand Down

0 comments on commit 37fe281

Please sign in to comment.