diff --git a/Sources/ConfidentialCore/Models/Configuration/Configuration.swift b/Sources/ConfidentialCore/Models/Configuration/Configuration.swift index cdbe835..5509da7 100644 --- a/Sources/ConfidentialCore/Models/Configuration/Configuration.swift +++ b/Sources/ConfidentialCore/Models/Configuration/Configuration.swift @@ -26,9 +26,9 @@ public struct Configuration: Equatable, Decodable { let container = try decoder.container(keyedBy: CodingKeys.self) self = .init( algorithm: try container.decode([String].self, forKey: .algorithm)[...], - defaultAccessModifier: try? container.decodeIfPresent(String.self, forKey: .defaultAccessModifier), - defaultNamespace: try? container.decodeIfPresent(String.self, forKey: .defaultNamespace), - implementationOnlyImport: try? container.decodeIfPresent(Bool.self, forKey: .implementationOnlyImport), + defaultAccessModifier: try container.decodeIfPresent(String.self, forKey: .defaultAccessModifier), + defaultNamespace: try container.decodeIfPresent(String.self, forKey: .defaultNamespace), + implementationOnlyImport: try container.decodeIfPresent(Bool.self, forKey: .implementationOnlyImport), secrets: try container.decode([Secret].self, forKey: .secrets)[...] ) } diff --git a/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift b/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift index 4234bd0..8964806 100644 --- a/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift +++ b/Tests/ConfidentialCoreTests/Models/Configuration/ConfigurationTests.swift @@ -56,6 +56,49 @@ final class ConfigurationTests: XCTestCase { ) } + func test_givenInvalidJSONEncodedConfiguration_whenDecodeWithJSONDecoder_thenThrowsError() { + // given + let invalidConfigurations = [ + """ + { + "algorithm": ["compress using lz4"] + } + """, + """ + { + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "defaultAccessModifier": [], + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "defaultNamespace": [], + "secrets": [{ "name": "secret", "value": "value" }] + } + """, + """ + { + "algorithm": ["compress using lz4"], + "implementationOnlyImport": 1, + "secrets": [{ "name": "secret", "value": "value" }] + } + """ + ].map { ($0, $0.data(using: .utf8)!) } + let decoder = JSONDecoder() + + // when & then + for (json, jsonData) in invalidConfigurations { + XCTAssertThrowsError(try decoder.decode(Configuration.self, from: jsonData), json) + } + } + func test_givenSecretSingleValue_whenJSONEncodeUnderlyingValue_thenResultEqualsJSONEncodedAssociatedValue() throws { // given let associatedValue = "test"