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

Override jsonschema property #63

Open
asiffer opened this issue Dec 9, 2022 · 0 comments
Open

Override jsonschema property #63

asiffer opened this issue Dec 9, 2022 · 0 comments

Comments

@asiffer
Copy link

asiffer commented Dec 9, 2022

Hi, I would like to know how to override/remove some jsonschema property.

In the example below, the MAC attribute is a slice of bytes and is represented as {"type":"string","contentEncoding":"base64"} in the generated schema. However, in my case I made a custom marshaller so as to turn this slice into a string. So I would like to remove the contentEncoding attribute in the schema.

I have tried to change its value but it is not reflected in the schema.

package main

import (
	"encoding/json"
	"fmt"
	"net"

	"github.com/invopop/jsonschema"
)

type NIC struct {
	Name string           `json:"name,omitempty"`
	MAC  net.HardwareAddr `json:"mac,omitempty" jsonschema:"contentEncoding=quoted-printable"`
}

func (nic *NIC) MarshalJSON() ([]byte, error) {
	type Alias NIC

	mac := ""
	if nic.MAC != nil {
		mac = nic.MAC.String()
	}

	return json.Marshal(&struct {
		MAC string `json:"mac,omitempty"`
		*Alias
	}{
		MAC:   mac,
		Alias: (*Alias)(nic),
	})
}

func main() {
	schema := jsonschema.Reflect(&NIC{})
	data, _ := schema.MarshalJSON()
	fmt.Println(string(data))
}
# output
{"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/NIC","$defs":{"HardwareAddr":{"type":"string","contentEncoding":"base64"},"NIC":{"properties":{"name":{"type":"string"},"mac":{"$ref":"#/$defs/HardwareAddr"}},"additionalProperties":false,"type":"object"}}}
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

1 participant