apple / apple/swift-openapi-generator

OpenAPIObjectContainer API weirdness, `init` validates value but then allows me to set anything into `.value`

Open
#782 3 comments 0 reactions 0 assignees View on GitHub
area/runtime kind/bug
Dominant language
Swift
Stars
2k
Forks
182
Avg merge
13h 28m
Merged PRs (30d)
5

Description

### Description

`OpenAPIObjectContainer`'s API has a bug:

```
public struct OpenAPIObjectContainer: Codable, Hashable, Sendable {
// public
public var value: [String: (any Sendable)?]
public init() { self.init(validatedValue: [:]) }
public init(unvalidatedValue: [String: (any Sendable)?]) throws {
try self.init(validatedValue: Self.tryCast(unvalidatedValue))
}

// internals
init(validatedValue value: [String: (any Sendable)?]) { self.value = value }
```

Basically the public `init`s force me to go through validation (and `throw`) but the setter for `self.value` accepts anything. So I can write

```swift
struct BadGuy: Sendable {}

var c = OpenAPIObjectContainer()
c.value["bad"] = BadGuy()
```

which now gives me a "validated" `OpenAPIObjectContainer` without validation.

### Reproduction

see above

### Package version(s)

runtime 1.8.0

### Expected behavior

won't let me set bad stuff

### Environment

6.2

### Additional information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the OpenAPIObjectContainer declaration and reproduce the mutation shown in the issue, focusing on the public value property and the two public initializers. Determine how the value API should preserve the validation invariant, then verify that inserting BadGuy cannot produce a validated container without validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, swift
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.