diff --git a/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml b/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml index 82dbcd913..2143d9c15 100644 --- a/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml +++ b/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml @@ -7,4 +7,32 @@ lib/netstandard2.0/Hl7.Fhir.Base.dll lib/net8.0/Hl7.Fhir.Base.dll + + CP0002 + M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String) + lib/net8.0/Hl7.Fhir.Base.dll + lib/net8.0/Hl7.Fhir.Base.dll + true + + + CP0002 + M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String) + lib/net8.0/Hl7.Fhir.Base.dll + lib/net8.0/Hl7.Fhir.Base.dll + true + + + CP0002 + M:Hl7.Fhir.Rest.ContentType.BuildContentType(Hl7.Fhir.Rest.ResourceFormat,System.String) + lib/netstandard2.0/Hl7.Fhir.Base.dll + lib/netstandard2.0/Hl7.Fhir.Base.dll + true + + + CP0002 + M:Hl7.Fhir.Rest.ContentType.BuildMediaType(Hl7.Fhir.Rest.ResourceFormat,System.String) + lib/netstandard2.0/Hl7.Fhir.Base.dll + lib/netstandard2.0/Hl7.Fhir.Base.dll + true + \ No newline at end of file diff --git a/src/Hl7.Fhir.Base/Rest/ContentType.cs b/src/Hl7.Fhir.Base/Rest/ContentType.cs index 573e8a0ed..0cc169911 100644 --- a/src/Hl7.Fhir.Base/Rest/ContentType.cs +++ b/src/Hl7.Fhir.Base/Rest/ContentType.cs @@ -102,8 +102,9 @@ public static ResourceFormat GetResourceFormatFromContentType(string? contentTyp /// /// Whether the body is xml or json. /// Optional. The version of FHIR to add to the header. - public static string BuildContentType(ResourceFormat format, string? fhirVersion = default) => - BuildMediaType(format, fhirVersion).ToString(); + /// Optional. Whether exclude charset. + public static string BuildContentType(ResourceFormat format, string? fhirVersion = default, bool excludeCharset = false) => + BuildMediaType(format, fhirVersion, excludeCharset).ToString(); /// /// Creates a for use in a Content-Type header, @@ -111,8 +112,9 @@ public static string BuildContentType(ResourceFormat format, string? fhirVersion /// /// Whether the body is xml or json. /// Optional. The version of FHIR to add to the header. + /// Optional. Whether exclude charset. /// Unsupported serialization. - 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 { @@ -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)) { diff --git a/src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs b/src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs index d27189120..3978eee48 100644 --- a/src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs +++ b/src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs @@ -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) { diff --git a/src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs b/src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs index 44fcea04c..4b07dd6cd 100644 --- a/src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs +++ b/src/Hl7.Fhir.Support.Poco.Tests/Rest/ContentTypeTests.cs @@ -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() { diff --git a/src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs b/src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs index 4320b580c..729378dce 100644 --- a/src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs +++ b/src/Hl7.Fhir.Support.Poco.Tests/Rest/RequestMessageTests.cs @@ -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"); }