This repository was archived by the owner on Jun 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.in
More file actions
328 lines (273 loc) · 7.12 KB
/
Copy pathREADME.in
File metadata and controls
328 lines (273 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
## Repository Relocation
Development of this project has moved to an
[open-source but not open-contribution](https://sqlite.org/copyright.html#notopencontrib)
model.
Source code and commits will remain publicly available perpetually, but issues
and/or pull requests will be rejected and/or ignored. Additionally, this project
will now only be available via a read-only mirror at:
https://codeberg.org/io7m-com/sumjack
## Sumjack
A JSON schema generator for [Jackson](https://jackson.tools) annotated Java types.
## Usage
### Building
```
$ mvn clean package
```
### Overview
The `sumjack` package is intended to produce [JSON schema](https://json-schema.org/)
documents from hierarchies of [Jackson](https://jackson.tools) annotated Java
types. The package provides a _generator_ that walks a tree of Java classes,
inspects the `@JsonProperty` annotations on the class methods, and then calls
registered _definitions_ to generate fragments of JSON schemas that are
eventually combined into a single schema document.
### Five Second Example
```
final var configuration =
SjGeneratorConfiguration.builder()
.setId(URI.create("urn:com.io7m.example:1.0"))
.setMapper(JsonMapper.shared())
.setRootType(Example.class)
.setSchemaVersion(SjSchemaVersion.DRAFT_2020_12)
.setTitle("Example 1.0")
.build();
final var generator =
SjGenerators.create(configuration);
generator.executeAndWrite(outputFile);
```
### Classes And Annotations
The `sumjack` package is opinionated about the kinds of classes for
which it will generate schemas. Specifically, the package expects to be
generating schemas for hierarchies of classes that are expressed in an
_algebraic sums and products_ style. That is, the classes for which schemas
are to be generated must be one of:
* Sealed interfaces (representing _sum_ types)
* Records (representing _product_ types)
* Primitives (`int`, `long`, etc.)
* Collections (`java.util.Map`, `java.util.Set`, `java.util.List`, `java.util.SortedMap`, `java.util.Optional`, etc.)
* Enums
The package is capable of producing schemas for any classes that fall into
the above categories without any kind of extra configuration or work needed
on the part of the user of the package. However, if a class does not fit into
one of these categories, then it is necessary to register a custom _definition_
in order to generate a schema for it.
### Definition
A _definition_ is a Java function that, when evaluated, produces a schema
object. For example, a _definition_ that produces a schema for the
`java.util.UUID` type might look like this:
```
SjGeneratorConfiguration configuration;
() -> {
final var mapper = configuration.mapper();
final var object = mapper.createObjectNode();
object.put("description", "An RFC 9562 UUID string.");
object.put("type", "string");
object.put("format", "uuid");
return object;
};
```
The `sumjack` package includes definitions for many of the standard Java
classes, although these must be enabled manually by adding them to the
`SjGeneratorConfiguration` value used to configure the generator:
```
final var configuration =
SjGeneratorConfiguration.builder()
.addDefinitions(SjUUID.UUID)
.addDefinitions(SjBigInteger.BIG_INTEGER)
.addDefinitions(SjOffsetDateTime.OFFSET_DATE_TIME)
.setId(URI.create("urn:com.io7m.example:1.0"))
.setMapper(JsonMapper.shared())
.setRootType(Example.class)
.setSchemaVersion(SjSchemaVersion.DRAFT_2020_12)
.setTitle("Example 1.0")
.build();
```
### Generation
#### Sealed Interfaces
A _sealed interface_ is expected to represent a _sum type_ and therefore
compiles down to a schema that matches `oneOf` a set of types. For example:
```
sealed interface SimpleBase0Type
permits SimpleBaseA,
SimpleBaseB,
SimpleBaseC
{
}
```
Produces a schema:
```
"SimpleBase0Type": {
"oneOf": [
{
"$ref": "#/$defs/SimpleBaseA"
},
{
"$ref": "#/$defs/SimpleBaseB"
},
{
"$ref": "#/$defs/SimpleBaseC"
}
]
},
```
#### Records
A _record_ is expected to represent a _product type_. The produced schema
for a record type is simply the `@JsonProperty` annotated constructor
parameters. For example:
```
public record Vector3(
@JsonProperty(value = "X", required = true)
double x,
@JsonProperty(value = "Y", required = true)
double y,
@JsonProperty(value = "Z", required = true)
double z)
{
}
```
Produces a schema:
```
"Vector3": {
"type": "object",
"properties": {
"X": {
"$ref": "#/$defs/double"
},
"Y": {
"$ref": "#/$defs/double"
},
"Z": {
"$ref": "#/$defs/double"
}
},
"required": [
"X",
"Y",
"Z"
]
},
"double": {
"type": "number",
"minimum": 2.2250738585072014E-308,
"maximum": 1.7976931348623157E308,
"description": "An IEEE764 64-bit floating point value."
}
```
#### Enums
Enums are translated to enumeration strings.
```
public enum TrafficLight
{
RED,
GREEN,
YELLOW
}
```
Produces a schema:
```
"TrafficLight": {
"type": "string",
"enum": [
"RED",
"GREEN",
"YELLOW"
]
}
```
#### Primitives
The Java primitives are, if the definitions in the `SjPrimitives` class
are registered, transformed to numeric and boolean types:
```
"boolean": {
"type": "boolean",
"description": "A boolean value."
},
"byte": {
"type": "number",
"description": "A primitive byte.",
"minimum": -128,
"maximum": 127
},
"char": {
"type": "number",
"description": "A primitive char.",
"minimum": 0,
"maximum": 65535
},
"double": {
"type": "number",
"minimum": 2.2250738585072014E-308,
"maximum": 1.7976931348623157E308,
"description": "An IEEE764 64-bit floating point value."
},
"float": {
"type": "number",
"minimum": 1.1754943508222875E-38,
"maximum": 3.4028234663852886E38,
"description": "An IEEE764 32-bit floating point value."
},
"int": {
"type": "number",
"description": "A primitive int.",
"minimum": -2147483648,
"maximum": 2147483647
},
"long": {
"type": "number",
"description": "A primitive long.",
"minimum": -9223372036854775808,
"maximum": 9223372036854775807
},
"short": {
"type": "number",
"description": "A primitive short.",
"minimum": -32768,
"maximum": 32767
}
```
#### Collections
Collection types are transformed to schema objects that match their types
as closely as possible. A `List<SimpleBaseA>` type, for example, becomes:
```
"List<SimpleBaseA>": {
"type": "array",
"items": {
"$ref": "#/$defs/SimpleBaseA"
}
},
"SimpleBaseA": {
"type": "object",
"properties": {},
"required": []
},
```
`java.util.Optional` values are effectively erased and become non-required
properties of the containing object:
```
public record SimpleContainsOptional(
@JsonProperty("Optional")
@JsonPropertyDescription("An element that might not be there.")
Optional<SimpleBaseA> elements)
{
}
```
Becomes:
```
"Optional<SimpleBaseA>": {
"$ref": "#/$defs/SimpleBaseA"
},
"SimpleBaseA": {
"type": "object",
"properties": {},
"required": []
},
"SimpleContainsOptional": {
"type": "object",
"properties": {
"Optional": {
"description": "An element that might not be there.",
"$ref": "#/$defs/Optional<SimpleBaseA>"
}
},
"required": []
},
```