Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is definitions or $defs? #65

Closed
tpoxa opened this issue Jan 9, 2023 · 1 comment
Closed

It is definitions or $defs? #65

tpoxa opened this issue Jan 9, 2023 · 1 comment

Comments

@tpoxa
Copy link
Contributor

tpoxa commented Jan 9, 2023

Trying to migrate from another similar library.
Have such of functionality broken. I used to think that they renamed to definitions to $defs to match $ref aren't they?

https://json-schema.org/draft/2020-12/json-schema-core.html

Thanks

@vearutop
Copy link
Member

vearutop commented Jan 9, 2023

This library is at draft-07 which has definitions.

But it is possible to use any custom place for definitions with a bit of instrumentation.

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

package main

import (
	"encoding/json"
	"fmt"
	"github.com/swaggest/jsonschema-go"
)

type S1 struct {
	A int `json:"a"`
	S S2  `json:"s"`
}

type S2 struct {
	B string `json:"b"`
}

func main() {
	r := jsonschema.Reflector{}

	defs := map[string]jsonschema.Schema{}

	s, _ := r.Reflect(S1{}, func(rc *jsonschema.ReflectContext) {
		rc.DefinitionsPrefix = "#/$defs/"
		rc.CollectDefinitions = func(name string, schema jsonschema.Schema) {
			defs[name] = schema
		}
	})

	s.WithExtraPropertiesItem("$defs", defs)

	j, _ := json.MarshalIndent(s, "", " ")

	fmt.Println(string(j))
}
{
 "properties": {
  "a": {
   "type": "integer"
  },
  "s": {
   "$ref": "#/$defs/S2"
  }
 },
 "type": "object",
 "$defs": {
  "S2": {
   "properties": {
    "b": {
     "type": "string"
    }
   },
   "type": "object"
  }
 }
}

@tpoxa tpoxa closed this as completed Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants