Skip to content

Commit 7bf0a00

Browse files
authored
Docs: update with rfl::Deprecated (#625)
1 parent b91fd65 commit 7bf0a00

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ struct Person {
367367
std::vector<Person>>
368368
children;
369369
float salary;
370+
rfl::Deprecated<"Use Salary Instead", "Wage in dollars", std::optional<int>> wage;
370371
};
371372

372373
const std::string json_schema = rfl::json::to_schema<Person>();
@@ -375,7 +376,7 @@ const std::string json_schema = rfl::json::to_schema<Person>();
375376
The resulting JSON schema looks like this:
376377
377378
```json
378-
{"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/Person","$defs":{"Person":{"type":"object","properties":{"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/Person"}},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"first_name":{"type":"string"},"last_name":{"type":"string"},"salary":{"type":"number"}},"required":["children","email","first_name","last_name","salary"]}}}
379+
{"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/Person","$defs":{"Person":{"type":"object","properties":{"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/Person"}},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"first_name":{"type":"string"},"last_name":{"type":"string"},"salary":{"type":"number"},"wage":{"type":"integer","description":"Wage in dollars","deprecated":true,"deprecationMessage":"Use Salary Instead"}},"required":["children","email","first_name","last_name","salary"]}}}
379380
```
380381

381382
Note that this is currently supported for JSON only, since most other formats do not support schemata in the first place.

docs/json_schema.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,55 @@ const std::string json_schema = rfl::json::to_schema<
192192
}
193193
}
194194
```
195+
196+
## Indicating deprecated fields
197+
198+
You can mark fields as deprecated in the JSON schema using `rfl::Deprecated`. This adds `"deprecated": true` and a `"deprecationMessage"` to the generated schema, along with a description.
199+
200+
`rfl::Deprecated` takes three template parameters: a deprecation message, a description, and the underlying type:
201+
202+
```cpp
203+
struct Person {
204+
rfl::Deprecated<"Use 'full_name' instead.", "The person's first name", std::optional<std::string>>
205+
std::optional<first_name>;
206+
rfl::Description<"The person's full name", std::string> full_name;
207+
float salary;
208+
};
209+
```
210+
211+
```cpp
212+
const std::string json_schema = rfl::json::to_schema<Person>(rfl::json::pretty);
213+
```
214+
215+
```json
216+
{
217+
"$schema": "https://json-schema.org/draft/2020-12/schema",
218+
"$ref": "#/$defs/Person",
219+
"$defs": {
220+
"Person": {
221+
"type": "object",
222+
"properties": {
223+
"first_name": {
224+
"type": "string",
225+
"description": "The person's first name",
226+
"deprecated": true,
227+
"deprecationMessage": "Use 'full_name' instead."
228+
},
229+
"full_name": {
230+
"type": "string",
231+
"description": "The person's full name"
232+
},
233+
"salary": {
234+
"type": "number"
235+
}
236+
},
237+
"required": [
238+
"full_name",
239+
"salary"
240+
]
241+
}
242+
}
243+
}
244+
```
245+
246+
`rfl::Deprecated` behaves like a thin wrapper around the underlying type, just like `rfl::Description`. You can access the underlying value using `.get()`, `.value()`, `operator()()`, or the assignment operator.

0 commit comments

Comments
 (0)