Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised error handling in Encodable conformance for Fields #538

Merged
merged 2 commits into from
Oct 28, 2022
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: 0 additions & 11 deletions .github/workflows/main-codecov.yml

This file was deleted.

16 changes: 10 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: test
on:
pull_request:
pull_request: { branches: ['*'] }
push: { branches: ['main'] }

env:
LOG_LEVEL: info
LOG_LEVEL: debug
SWIFT_DETERMINISTIC_HASHING: 1
POSTGRES_HOSTNAME: 'psql-a'
POSTGRES_HOSTNAME_A: 'psql-a'
Expand Down Expand Up @@ -37,8 +38,9 @@ jobs:

# Check for API breakage versus main
api-breakage:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
container: swift:5.6-focal
container: swift:5.7-jammy
steps:
- name: Check out package
uses: actions/checkout@v3
Expand All @@ -51,8 +53,9 @@ jobs:
run: swift package diagnose-api-breaking-changes origin/main

linux-integration:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
container: swift:5.6-focal
container: swift:5.7-jammy
services:
mysql-a:
image: mysql:8.0
Expand All @@ -61,13 +64,13 @@ jobs:
image: mysql:8.0
env: { MYSQL_ALLOW_EMPTY_PASSWORD: true, MYSQL_USER: test_username, MYSQL_PASSWORD: test_password, MYSQL_DATABASE: test_database }
psql-a:
image: postgres:14
image: postgres:15
env: {
POSTGRES_USER: test_username, POSTGRES_PASSWORD: test_password, POSTGRES_DB: test_database,
POSTGRES_HOST_AUTH_METHOD: scram-sha-256, POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
}
psql-b:
image: postgres:14
image: postgres:15
env: {
POSTGRES_USER: test_username, POSTGRES_PASSWORD: test_password, POSTGRES_DB: test_database,
POSTGRES_HOST_AUTH_METHOD: scram-sha-256, POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
Expand Down Expand Up @@ -108,6 +111,7 @@ jobs:
- name: Run tests
run: swift test --package-path ${{ matrix.dependent }}

# also serves as code coverage baseline update
unit-tests:
uses: vapor/ci/.github/workflows/run-unit-tests.yml@reusable-workflows
with:
Expand Down
55 changes: 50 additions & 5 deletions Sources/FluentBenchmark/Tests/ModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extension FluentBenchmarker {
try self.testModel_jsonColumn()
try self.testModel_hasChanges()
try self.testModel_outputError()
if self.database is SQLDatabase {
// Broken in Mongo at this time
try self.testModel_useOfFieldsWithoutGroup()
}
}

private func testModel_uuid() throws {
Expand Down Expand Up @@ -163,11 +167,52 @@ extension FluentBenchmarker {
}

private func testModel_outputError() throws {
let foo = Foo()
do {
try foo.output(from: BadFooOutput())
} catch {
XCTAssert("\(error)".contains("id"))
try runTest(#function, []) {
let foo = Foo()
do {
try foo.output(from: BadFooOutput())
} catch {
XCTAssert("\(error)".contains("id"))
}
}
}

private func testModel_useOfFieldsWithoutGroup() throws {
try runTest(#function, []) {
final class Contained: Fields {
@Field(key: "something") var something: String
@Field(key: "another") var another: Int
init() {}
}
final class Enclosure: Model {
static let schema = "enclosures"
@ID(custom: .id) var id: Int?
@Field(key: "primary") var primary: Contained
@Field(key: "additional") var additional: [Contained]
init() {}

struct Migration: FluentKit.Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema(Enclosure.schema)
.field(.id, .int, .required, .identifier(auto: true))
.field("primary", .json, .required)
.field("additional", .array(of: .json), .required)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> { database.schema(Enclosure.schema).delete() }
}
}

try Enclosure.Migration().prepare(on: self.database).wait()

let enclosure = Enclosure()
enclosure.primary = .init()
enclosure.primary.something = ""
enclosure.primary.another = 0
enclosure.additional = []
try enclosure.save(on: self.database).wait()

try! Enclosure.Migration().revert(on: self.database).wait()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentKit/Model/Fields+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Fields {
for (key, property) in self.codableProperties where !property.skipPropertyEncoding {
do {
try property.encode(to: container.superEncoder(forKey: key))
} catch {
} catch let error where error is EncodingError { // trapping all errors breaks value handling logic in database driver layers
throw EncodingError.invalidValue(property.anyValue ?? "null", .init(
codingPath: container.codingPath + [key],
debugDescription: "Could not encode property",
Expand Down