Skip to content

Commit

Permalink
Add ability to ignore struct types in schema
Browse files Browse the repository at this point in the history
Replaces alecthomas#32, thanks to alecthomas#35 fixing the other part of it anyway.
  • Loading branch information
abayer authored and rawlingsj committed Nov 30, 2020
1 parent eff3f6c commit 1e14249
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
115 changes: 115 additions & 0 deletions fixtures/ignore_type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"$schema": "http:\/\/json-schema.org\/draft-04\/schema#",
"$ref": "#\/definitions\/TestUser",
"definitions": {
"GrandfatherType": {
"properties": {},
"additionalProperties": true,
"type": "object"
},
"TestUser": {
"required": [
"some_base_property",
"some_base_property_yaml",
"grand",
"SomeUntaggedBaseProperty",
"id",
"name",
"TestFlag",
"age",
"email"
],
"properties": {
"SomeUntaggedBaseProperty": {
"type": "boolean"
},
"TestFlag": {
"type": "boolean"
},
"age": {
"maximum": 120,
"exclusiveMaximum": true,
"minimum": 18,
"exclusiveMinimum": true,
"type": "integer"
},
"birth_date": {
"type": "string",
"format": "date-time"
},
"email": {
"type": "string",
"format": "email"
},
"feeling": {
"oneOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"friends": {
"items": {
"type": "integer"
},
"type": "array",
"description": "list of IDs, omitted when empty"
},
"grand": {
"$schema": "http:\/\/json-schema.org\/draft-04\/schema#",
"$ref": "#\/definitions\/GrandfatherType"
},
"id": {
"type": "integer"
},
"name": {
"maxLength": 20,
"minLength": 1,
"pattern": ".*",
"type": "string",
"title": "the name",
"description": "this is a property",
"default": "alex",
"examples": [
"joe",
"lucy"
]
},
"network_address": {
"type": "string",
"format": "ipv4"
},
"photo": {
"type": "string",
"media": {
"binaryEncoding": "base64"
}
},
"some_base_property": {
"type": "integer"
},
"some_base_property_yaml": {
"type": "integer"
},
"tags": {
"patternProperties": {
".*": {
"additionalProperties": true,
"type": "object"
}
},
"type": "object"
},
"website": {
"type": "string",
"format": "uri"
}
},
"additionalProperties": false,
"type": "object"
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module github.com/alecthomas/jsonschema
module github.com/rawlingsj/jsonschema
20 changes: 20 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type Reflector struct {
// ExpandedStruct will cause the toplevel definitions of the schema not
// be referenced itself to a definition.
ExpandedStruct bool

// IgnoredTypes defines a slice of types that should be ignored in the schema,
// switching to just allowing additional properties instead.
IgnoredTypes []interface{}
}

// Reflect reflects to Schema from a value.
Expand Down Expand Up @@ -246,6 +250,22 @@ func (r *Reflector) reflectTypeToSchema(definitions Definitions, t reflect.Type)

// Refects a struct to a JSON Schema type.
func (r *Reflector) reflectStruct(definitions Definitions, t reflect.Type) *Type {
for _, ignored := range r.IgnoredTypes {
if reflect.TypeOf(ignored) == t {
st := &Type{
Type: "object",
Properties: map[string]*Type{},
AdditionalProperties: []byte("true"),
}
definitions[t.Name()] = st

return &Type{
Version: Version,
Ref: "#/definitions/" + t.Name(),
}

}
}
st := &Type{
Type: "object",
Properties: map[string]*Type{},
Expand Down
1 change: 1 addition & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var schemaGenerationTests = []struct {
{&Reflector{AllowAdditionalProperties: true}, "fixtures/allow_additional_props.json"},
{&Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/required_from_jsontags.json"},
{&Reflector{ExpandedStruct: true}, "fixtures/defaults_expanded_toplevel.json"},
{&Reflector{IgnoredTypes: []interface{}{GrandfatherType{}}}, "fixtures/ignore_type.json"},
}

func TestSchemaGeneration(t *testing.T) {
Expand Down

0 comments on commit 1e14249

Please sign in to comment.