Commit 2196fed
authored
fix: infer - missing nullable type for non Go types (#42)
When providing a struct containing an attribute where the type is a pointer to a
non-native type, the infer analysis wouldn't set the output schema type as
nullable for this attribute.
Example:
```go
type example struct {
Value *time.Time `json:"value"`
}
func main() {
schema, err := jsonschema.For[example](nil)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate JSON schema: %v", err)
os.Exit(1)
}
encoded, err := json.Marshal(schema)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to encode JSON schema: %v", err)
os.Exit(1)
}
fmt.Println(string(encoded))
}
```
This would generate:
```json
{"type":"object","required":["value"],"properties":{"value":{"type":"string"}},"additionalProperties":false}
```
Using the `TypeSchemas` option will have the same result, since only the
element is analysed [1]:
```go
schema, err := jsonschema.For[example](&jsonschema.ForOptions{
TypeSchemas: map[reflect.Type]*jsonschema.Schema{
reflect.TypeFor[*time.Time](): {
Types: []string{"null", "string"},
},
},
})
```
With this patch the null flag will be checked even when there's a match with
pre-registered types, resulting on the following output:
```json
{"type":"object","required":["value"],"properties":{"value":{"type":["null","string"]}},"additionalProperties":false}
```
Resolves #41
[1] https://github.com/google/jsonschema-go/blob/3ba200528d086ef03a2d8e8d4f7d7146400e342f/jsonschema/infer.go#L1141 parent ad34a93 commit 2196fed
2 files changed
+51
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
128 | 132 | | |
129 | 133 | | |
130 | 134 | | |
131 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
132 | 145 | | |
133 | 146 | | |
134 | 147 | | |
| |||
332 | 345 | | |
333 | 346 | | |
334 | 347 | | |
335 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
336 | 353 | | |
337 | 354 | | |
338 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
| 98 | + | |
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| |||
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
116 | | - | |
| 117 | + | |
| 118 | + | |
117 | 119 | | |
118 | 120 | | |
119 | 121 | | |
| |||
125 | 127 | | |
126 | 128 | | |
127 | 129 | | |
| 130 | + | |
128 | 131 | | |
129 | 132 | | |
130 | | - | |
| 133 | + | |
131 | 134 | | |
132 | 135 | | |
133 | 136 | | |
| |||
220 | 223 | | |
221 | 224 | | |
222 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
223 | 229 | | |
224 | | - | |
225 | | - | |
226 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
227 | 235 | | |
228 | | - | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
229 | 241 | | |
230 | 242 | | |
231 | 243 | | |
| |||
239 | 251 | | |
240 | 252 | | |
241 | 253 | | |
| 254 | + | |
| 255 | + | |
242 | 256 | | |
243 | 257 | | |
244 | 258 | | |
| |||
248 | 262 | | |
249 | 263 | | |
250 | 264 | | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
255 | 275 | | |
256 | | - | |
| 276 | + | |
257 | 277 | | |
258 | 278 | | |
259 | 279 | | |
| |||
0 commit comments