Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/976aa8d4-d143-4f7b-9bf5-762b166d7fb5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Extensions.Configuration.SystemsManager",
"Type": "Patch",
"ChangelogMessages": [
"Removed content type check for app config. We now always try to parse json first"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
<None Include="../../icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Amazon.Extensions.Configuration.SystemsManager.Tests,PublicKey=0024000004800000940000000602000000240000525341310004000001000100db5f59f098d27276c7833875a6263a3cc74ab17ba9a9df0b52aedbe7252745db7274d5271fd79c1f08f668ecfa8eaab5626fa76adc811d3c8fc55859b0d09d3bc0a84eecd0ba891f2b8a2fc55141cdcc37c2053d53491e650a479967c3622762977900eddbf1252ed08a2413f00a28f3a0752a81203f03ccb7f684db373518b4" />
</ItemGroup>

<PropertyGroup>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The System.Text.Json using statement is added but JsonException is the only type being used from this namespace. Consider if this import is necessary or if the existing JsonConfigurationParser.Parse method already handles JSON exceptions appropriately.

Suggested change
using System.Text.Json;

Copilot uses AI. Check for mistakes.

using System.Threading;
using System.Threading.Tasks;
using Amazon.AppConfigData;
Expand Down Expand Up @@ -162,20 +163,17 @@ private async Task<string> GetInitialConfigurationTokenAsync(IAmazonAppConfigDat
return (await appConfigClient.StartConfigurationSessionAsync(request).ConfigureAwait(false)).InitialConfigurationToken;
}

private static IDictionary<string, string> ParseConfig(string contentType, Stream configuration)
internal static IDictionary<string, string> ParseConfig(string contentType, Stream configuration)
{
// Content-Type has format "media-type; charset" or "media-type; boundary" (for multipart entities).
if (contentType != null)
try
{
contentType = contentType.Split(';')[0];
return JsonConfigurationParser.Parse(configuration);
}

switch (contentType)
catch (JsonException ex)
{
case "application/json":
return JsonConfigurationParser.Parse(configuration);
default:
throw new NotImplementedException($"Not implemented AppConfig type: {contentType}");
throw new InvalidOperationException(
$"Failed to parse AppConfig content as JSON. Content-Type was '{contentType}'. {ex.Message}",
ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,42 @@ public async Task JsonWithCharsetConfiguration()
}
}

[Fact]
public async Task AppConfigWithOctetStreamContentType()
{
var configSettings = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "database:connectionString", "server=localhost;database=test" },
{ "api:timeout", "30" }
};

// Create AppConfig with application/octet-stream content type (simulates Parameter Store backend)
(string applicationId, string environmentId, string configProfileId) =
await CreateAppConfigResourcesAsync("OctetStreamTest", configSettings, "application/octet-stream");

try
{
var builder = new ConfigurationBuilder()
.AddAppConfig(applicationId, environmentId, configProfileId,
new AWSOptions { Region = RegionEndpoint.USWest2 },
TimeSpan.FromSeconds(5));

var configuration = builder.Build();

// Verify configuration loads correctly despite application/octet-stream content type
Assert.Equal("value1", configuration["key1"]);
Assert.Equal("value2", configuration["key2"]);
Assert.Equal("server=localhost;database=test", configuration["database:connectionString"]);
Assert.Equal("30", configuration["api:timeout"]);
}
finally
{
await CleanupAppConfigResourcesAsync(applicationId, environmentId, configProfileId);
}
}

private async Task CleanupAppConfigResourcesAsync(string applicationId, string environmentId, string configProfileId)
{
await _appConfigClient.DeleteEnvironmentAsync(new DeleteEnvironmentRequest {ApplicationId = applicationId, EnvironmentId = environmentId });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Amazon.Extensions.Configuration.SystemsManager.AppConfig;
using Xunit;

namespace Amazon.Extensions.Configuration.SystemsManager.Tests
{
public class AppConfigProcessorTests
{
[Fact]
public void ParseConfig_ApplicationJson_ParsesSuccessfully()
{
var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/json", stream);

Assert.Equal("value1", result["key1"]);
Assert.Equal("value2", result["key2"]);
}

[Fact]
public void ParseConfig_ApplicationOctetStream_ParsesJsonSuccessfully()
{
var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/octet-stream", stream);

Assert.Equal("value1", result["key1"]);
Assert.Equal("value2", result["key2"]);
}

[Fact]
public void ParseConfig_ApplicationJsonWithCharset_ParsesSuccessfully()
{
var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/json; charset=utf-8", stream);

Assert.Equal("value1", result["key1"]);
Assert.Equal("value2", result["key2"]);
}

[Fact]
public void ParseConfig_UnknownContentType_ParsesJsonSuccessfully()
{
var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/unknown", stream);

Assert.Equal("value1", result["key1"]);
Assert.Equal("value2", result["key2"]);
}

[Fact]
public void ParseConfig_NullContentType_ParsesJsonSuccessfully()
{
var jsonContent = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig(null, stream);

Assert.Equal("value1", result["key1"]);
Assert.Equal("value2", result["key2"]);
}

[Fact]
public void ParseConfig_NestedJson_ParsesSuccessfully()
{
var jsonContent = "{\"section1\":{\"key1\":\"value1\",\"key2\":\"value2\"},\"section2\":{\"key3\":\"value3\"}}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/octet-stream", stream);

Assert.Equal("value1", result["section1:key1"]);
Assert.Equal("value2", result["section1:key2"]);
Assert.Equal("value3", result["section2:key3"]);
}

[Fact]
public void ParseConfig_InvalidJson_ThrowsInvalidOperationException()
{
var invalidJsonContent = "{ invalid json content }";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(invalidJsonContent));

var exception = Assert.Throws<InvalidOperationException>(() =>
AppConfigProcessor.ParseConfig("application/json", stream));

Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message);
Assert.Contains("Content-Type was 'application/json'", exception.Message);
}

[Fact]
public void ParseConfig_InvalidJsonWithOctetStream_ThrowsInvalidOperationException()
{
var invalidJsonContent = "not json at all";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(invalidJsonContent));

var exception = Assert.Throws<InvalidOperationException>(() =>
AppConfigProcessor.ParseConfig("application/octet-stream", stream));

Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message);
Assert.Contains("Content-Type was 'application/octet-stream'", exception.Message);
}

[Fact]
public void ParseConfig_EmptyJson_ReturnsEmptyDictionary()
{
var jsonContent = "{}";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent));

var result = AppConfigProcessor.ParseConfig("application/json", stream);

Assert.Empty(result);
}

[Fact]
public void ParseConfig_EmptyStream_ThrowsInvalidOperationException()
{
using var stream = new MemoryStream();

var exception = Assert.Throws<InvalidOperationException>(() =>
AppConfigProcessor.ParseConfig("application/json", stream));

Assert.Contains("Failed to parse AppConfig content as JSON", exception.Message);
}
}
}