Add option to allow additionalProperties on generated schemas
- Dominant language
- Go
- Stars
- 478
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
## Problem
`jsonschema.ForType` sets `additionalProperties: false` on all object types. There is no way to opt out of this behavior via `ForOptions`.
This is a problem for protocols that send additional fields alongside typed arguments. For example, the [Model Context Protocol](https://github.com/modelcontextprotocol/go-sdk) uses `jsonschema.ForType` to generate tool input schemas. When a protocol envelope includes fields not defined in the tool's Go struct, the strict `additionalProperties: false` causes validation to reject the entire request.
## Use case
The [Ad Context Protocol (AdCP)](https://adcontextprotocol.org) sends protocol-level fields like `adcp_major_version` alongside tool arguments. These fields are not part of any individual tool's schema but are part of the protocol envelope. The strict `additionalProperties: false` rejects these requests before they reach the handler.
The current workaround is to walk the generated schema tree and set `AdditionalProperties = nil` on all object types after generation, which is fragile.
## Proposed solution
Add an option to `ForOptions` that controls whether `additionalProperties: false` is set on generated schemas:
```go
type ForOptions struct {
// ...existing fields...
AllowAdditionalProperties bool
}
```
When `AllowAdditionalProperties` is true, the generated schema would omit `additionalProperties: false`, allowing objects to accept fields not explicitly defined in the Go struct.
## Context
- Filed at the suggestion of @jba in modelcontextprotocol/go-sdk#892
- Current workaround: https://github.com/adcontextprotocol/adcp-go/blob/main/adcp/addtool.go
Contributor guide
Assessment
This issue has not been assessed yet.