Skip to content

Add validating setters to OpenAPI dynamic value containers - #214

Merged
czechboy0 merged 4 commits into
apple:mainfrom
shubhransh-gupta:fix/object-container-validating-setter
Oct 2, 2026
Merged

czechboy0 merged 4 commits into
apple:mainfrom
shubhransh-gupta:fix/object-container-validating-setter

Conversation

@shubhransh-gupta

Copy link
Copy Markdown
Contributor

Motivation

OpenAPIValueContainer, OpenAPIObjectContainer, and OpenAPIArrayContainer previously exposed var value with an unvalidated public setter. This made it possible to mutate containers with unsupported types that would be rejected by their initializers.

Resolves apple/swift-openapi-generator#782.

Modifications

  • Deprecate the direct public setter on value across OpenAPIValueContainer, OpenAPIObjectContainer, and OpenAPIArrayContainer, backed by a private stored property _value.
  • Add throwing mutating methods setValidatedValue and setValue(validating:) to replace container values with runtime type validation.
  • On OpenAPIObjectContainer, add setValidatedValue(_:forKey:) and setValue(validating:forKey:) to support validated key-level mutations.
  • Add unit tests in Test_OpenAPIValue.swift verifying accepted values, rejected unsupported types, and preservation of state on failure.
  • Add tests in Test_Deprecated.swift verifying backward-compatible behavior of the deprecated setters.

Result

Callers have validating mutation APIs to safely update dynamic value containers, and direct assignments to value produce deprecation warnings directing callers to the validating methods.

Test Plan

Executed test suite via swift test: 239 tests executed, 0 failures.
Formatted and verified with swift-format lint.

### Motivation

OpenAPIValueContainer, OpenAPIObjectContainer, and OpenAPIArrayContainer previously exposed var value with an unvalidated public setter. This allowed callers to mutate containers with unsupported types that would have been rejected by the initializers.

Resolves apple/swift-openapi-generator#782.

### Modifications

- Deprecate direct mutation via the setter on value across OpenAPIValueContainer, OpenAPIObjectContainer, and OpenAPIArrayContainer, backed by a private stored property _value.
- Add throwing mutating methods setValidatedValue and setValue(validating:) to replace or update container values with runtime type validation.
- On OpenAPIObjectContainer, add setValidatedValue(_:forKey:) and setValue(validating:forKey:) to support validated key-level mutations.
- Add comprehensive unit tests in Test_OpenAPIValue.swift verifying accepted values, rejected unsupported types, and preservation of state on failure.
- Add tests in Test_Deprecated.swift verifying backward-compatible behavior of the deprecated setters.

### Result

Callers have validating mutation APIs to safely update dynamic value containers, and direct assignments to value produce deprecation warnings directing callers to the validating methods.

### Test Plan

Executed test suite via swift test: 239 tests executed, 0 failures.
Formatted and verified with swift-format lint.

@czechboy0 czechboy0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, @shubhransh-gupta 🙏

@czechboy0
czechboy0 enabled auto-merge (squash) October 2, 2026 13:57
@czechboy0 czechboy0 added the 🆕 semver/minor Adds new public API. label Oct 2, 2026
@czechboy0
czechboy0 merged commit 7fd86e1 into apple:main Oct 2, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disallow setting an invalid value into OpenAPIObjectContainer.value

2 participants