Skip to content

How to generate nullable struct references? #78

Answered by vearutop
benjamin-ratier asked this question in Q&A
Discussion options

You must be logged in to vote

I think jsonschema.InterceptNullability would be suitable for this, please check an example.

https://go.dev/play/p/Dt5HxQ8XJUp

	jr.DefaultOptions = append(jr.DefaultOptions, jsonschema.InterceptNullability(func(params jsonschema.InterceptNullabilityParams) {
		if params.RefDef != nil && params.Type.Kind() == reflect.Ptr {
			params.Schema.WithAllOf(jsonschema.Null.ToSchemaOrBool(), params.OrigSchema.ToSchemaOrBool())
			params.Schema.Ref = nil
		}
	}))
components:
  schemas:
    Bar:
      properties:
        foo:
          allOf:
          - nullable: true
          - $ref: '#/components/schemas/FooStruct'
          type: object
      type: object

or

	jr.DefaultOptions = append(jr.Defaul…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@benjamin-ratier
Comment options

Answer selected by benjamin-ratier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants