You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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#L114
0 commit comments