GREsau / GREsau/okapi

Enforce strict key values for BTreeMap with enum key

Open
#128 5 comments 0 reactions 0 assignees View on GitHub
schemars
Dominant language
Rust
Stars
740
Forks
127
PR merge metrics
No merged PRs in 30d

Description

Whenever a schema is generated for a BTreeMap with an enum value as the key, the result allows _any_ string for the key. For example:
```rust
#[derive(serde::Serialize, JsonSchema, Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
enum Thing {
Option1,
Option2,
}
#[derive(serde::Serialize, JsonSchema)]
struct Response {
reply: BTreeMap,
}
```

generates:
```JSON
"components": {
"schemas": {
"Response": {
"type": "object",
"required": [
"reply"
],
"properties": {
"reply": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
```

This allows the `reply` object to have any key, when it should really be limited to the enum values. Something like:
```JSON
"components": {
"schemas": {
"Response": {
"type": "object",
"required": [
"reply"
],
"properties": {
"reply": {
"oneOf": [
{
"type": "object",
"required": [
"Option1"
],
"properties": {
"Option1": {
"type": "string"
}
}
},
{
"type": "object",
"required": [
"Option2"
],
"properties": {
"Option2": {
"type": "string"
}
},
"maxProperties": 1
}
]
}
}
}
}
}
```

I don't know if there's any other way to limit the possible key values for an object, other than using the `oneOf` property, but being able to limit potential keys of a spec using a BTreeMap would be really helpful

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.