-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://github.com/invopop/jsonschema/examples/user", | ||
"$ref": "#/$defs/User", | ||
"$defs": { | ||
"NamedPets": { | ||
"additionalProperties": { | ||
"$ref": "#/$defs/Pet" | ||
}, | ||
"type": "object", | ||
"description": "NamedPets is a map of animal names to pets." | ||
}, | ||
"Pet": { | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"title": "Name", | ||
"description": "Name of the animal." | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"type": "object", | ||
"required": [ | ||
"name" | ||
], | ||
"description": "Pet defines the user's fury friend." | ||
}, | ||
"Pets": { | ||
"items": { | ||
"$ref": "#/$defs/Pet" | ||
}, | ||
"type": "array", | ||
"description": "Pets is a collection of Pet objects." | ||
}, | ||
"Plant": { | ||
"properties": { | ||
"variant": { | ||
"type": "string", | ||
"title": "Variant", | ||
"description": "This comment will be used" | ||
}, | ||
"multicellular": { | ||
"type": "boolean", | ||
"title": "Multicellular", | ||
"description": "Multicellular is true if the plant is multicellular" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"type": "object", | ||
"required": [ | ||
"variant" | ||
], | ||
"description": "Plant represents the plants the user might have and serves as a test\nof structs inside a `type` set." | ||
}, | ||
"User": { | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"description": "Unique sequential identifier." | ||
}, | ||
"name": { | ||
"type": "string", | ||
"maxLength": 20, | ||
"minLength": 1, | ||
"pattern": ".*", | ||
"title": "the name", | ||
"description": "this is a property", | ||
"default": "alex", | ||
"examples": [ | ||
"joe", | ||
"lucy" | ||
] | ||
}, | ||
"friends": { | ||
"items": { | ||
"type": "integer" | ||
}, | ||
"type": "array", | ||
"description": "list of IDs, omitted when empty" | ||
}, | ||
"tags": { | ||
"type": "object" | ||
}, | ||
"pets": { | ||
"$ref": "#/$defs/Pets", | ||
"description": "An array of pets the user cares for." | ||
}, | ||
"named_pets": { | ||
"$ref": "#/$defs/NamedPets", | ||
"description": "Set of animal names to pets" | ||
}, | ||
"plants": { | ||
"items": { | ||
"$ref": "#/$defs/Plant" | ||
}, | ||
"type": "array", | ||
"title": "Plants", | ||
"description": "Set of plants that the user likes" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"type": "object", | ||
"required": [ | ||
"id", | ||
"name", | ||
"pets", | ||
"named_pets", | ||
"plants" | ||
], | ||
"description": "User is used as a base to provide tests for comments.\nDon't forget to checkout the nested path." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/invopop/jsonschema/examples" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCommentsSchemaGeneration(t *testing.T) { | ||
tests := []struct { | ||
typ any | ||
reflector *Reflector | ||
fixture string | ||
}{ | ||
{&examples.User{}, prepareCommentReflector(t), "fixtures/go_comments.json"}, | ||
{&examples.User{}, prepareCommentReflector(t, WithFullComment()), "fixtures/go_comments_full.json"}, | ||
} | ||
for _, tt := range tests { | ||
name := strings.TrimSuffix(filepath.Base(tt.fixture), ".json") | ||
t.Run(name, func(t *testing.T) { | ||
compareSchemaOutput(t, | ||
tt.fixture, tt.reflector, tt.typ, | ||
) | ||
}) | ||
} | ||
} | ||
|
||
func prepareCommentReflector(t *testing.T, opts ...CommentOption) *Reflector { | ||
t.Helper() | ||
r := new(Reflector) | ||
err := r.AddGoComments("github.com/invopop/jsonschema", "./examples", opts...) | ||
require.NoError(t, err, "did not expect error while adding comments") | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters