Skip to content

Commit

Permalink
JSON Schema: Ignore fields via comment (#14)
Browse files Browse the repository at this point in the history
* JSON Schema: Ignore fields via comment

* checkpoint

* checkpoint
  • Loading branch information
rodaine authored May 8, 2024
1 parent 9c12400 commit a876f2b
Show file tree
Hide file tree
Showing 25 changed files with 218 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ test: build $(BIN)/jv ## Run unit tests
golden: generate
rm -rf internal/testdata/pubsub
rm -rf internal/testdata/jsonschema
buf build ./internal/proto -o -#format=json > ./internal/testdata/codegenrequest/input.json
go run internal/cmd/pubsub-generate-testdata/main.go internal/testdata/pubsub
go run internal/cmd/jsonschema-generate-testdata/main.go internal/testdata/jsonschema
buf build ./internal/proto --exclude-source-info -o -#format=json > ./internal/testdata/codegenrequest/input.json

.PHONY: build
build: generate ## Build all packages
Expand Down Expand Up @@ -89,7 +89,7 @@ $(BIN)/license-header: $(BIN) Makefile
go install github.com/bufbuild/buf/private/pkg/licenseheader/cmd/license-header@latest

$(BIN)/golangci-lint: $(BIN) Makefile
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.1

$(BIN)/jv: $(BIN) Makefile
go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest
6 changes: 5 additions & 1 deletion internal/cmd/jsonschema-generate-testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func run() error {
}
outputDir := os.Args[1]

for _, testDesc := range golden.GetTestDescriptors() {
testDescs, err := golden.GetTestDescriptors("./internal/testdata")
if err != nil {
return err
}
for _, testDesc := range testDescs {
// Generate the JSON schema
result := jsonschema.Generate(testDesc)

Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/pubsub-generate-testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func run() error {
}

// Generate the testdata
for _, testDesc := range golden.GetTestDescriptors() {
testDescs, err := golden.GetTestDescriptors("./internal/testdata")
if err != nil {
return err
}
for _, testDesc := range testDescs {
filePath := filepath.Join(dirPath, fmt.Sprintf("%s.%s", testDesc.FullName(), pubsub.FileExtension))
data, err := pubsub.Generate(testDesc)
if err != nil {
Expand Down
130 changes: 107 additions & 23 deletions internal/gen/proto/buf/protoschema/test/v1/test_cases.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/proto/buf/protoschema/test/v1/test_cases.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ message CustomOptions {
}];
}
}

message IgnoreField {
string string_field = 1; // jsonschema:ignore
// jsonschema:ignore
int32 int32_field = 2;
bool bool_field = 3;
}
45 changes: 37 additions & 8 deletions internal/protoschema/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,50 @@ import (
"fmt"
"io"
"os"
"path/filepath"

testv1 "github.com/bufbuild/protoschema-plugins/internal/gen/proto/buf/protoschema/test/v1"
"github.com/bufbuild/protoschema-plugins/internal/gen/proto/bufext/cel/expr/conformance/proto3"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
)

// GetTestDescriptors returns the test descriptors that were generated from the ./internal/proto
// directory.
func GetTestDescriptors() []protoreflect.MessageDescriptor {
return []protoreflect.MessageDescriptor{
(&proto3.TestAllTypes{}).ProtoReflect().Descriptor(),
(&proto3.NestedTestAllTypes{}).ProtoReflect().Descriptor(),
(&testv1.NestedReference{}).ProtoReflect().Descriptor(),
(&testv1.CustomOptions{}).ProtoReflect().Descriptor(),
func GetTestDescriptors(testdataPath string) ([]protoreflect.MessageDescriptor, error) {
inputPath := filepath.Join(filepath.FromSlash(testdataPath), "codegenrequest", "input.json")
input, err := os.ReadFile(inputPath)
if err != nil {
return nil, fmt.Errorf("failed to open input file descritpor set at %q: %w", inputPath, err)
}
fdset := &descriptorpb.FileDescriptorSet{}
if err = (&protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(input, fdset); err != nil {
return nil, fmt.Errorf("failed to parse file descriptor set at %q: %w", inputPath, err)
}
files, err := protodesc.NewFiles(fdset)
if err != nil {
return nil, fmt.Errorf("failed to link file descriptor set at %q: %w", inputPath, err)
}
types := dynamicpb.NewTypes(files)

fqns := []protoreflect.FullName{
"bufext.cel.expr.conformance.proto3.TestAllTypes",
"bufext.cel.expr.conformance.proto3.NestedTestAllTypes",
"buf.protoschema.test.v1.NestedReference",
"buf.protoschema.test.v1.CustomOptions",
"buf.protoschema.test.v1.IgnoreField",
}

msgs := make([]protoreflect.MessageDescriptor, len(fqns))
for i, fqn := range fqns {
mType, err := types.FindMessageByName(fqn)
if err != nil {
return nil, fmt.Errorf("failed to find message %q: %w", fqn, err)
}
msgs[i] = mType.Descriptor()
}
return msgs, nil
}

// CheckGolden checks the golden file exists and matches the given data.
Expand Down
10 changes: 10 additions & 0 deletions internal/protoschema/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (p *jsonSchemaGenerator) generateDefault(result map[string]interface{}, des
var properties = make(map[string]interface{})
for i := range desc.Fields().Len() {
field := desc.Fields().Get(i)
if p.shouldIgnoreField(field) {
continue
}
name := string(field.Name())
properties[name] = p.generateField(field)
}
Expand Down Expand Up @@ -262,3 +265,10 @@ func (p *jsonSchemaGenerator) makeWktGenerators() map[protoreflect.FullName]func
result["google.protobuf.UInt64Value"] = p.generateWrapperValidation
return result
}

func (p *jsonSchemaGenerator) shouldIgnoreField(fdesc protoreflect.FieldDescriptor) bool {
const ignoreComment = "jsonschema:ignore"
srcLoc := fdesc.ParentFile().SourceLocations().ByDescriptor(fdesc)
return strings.Contains(srcLoc.LeadingComments, ignoreComment) ||
strings.Contains(srcLoc.TrailingComments, ignoreComment)
}
4 changes: 3 additions & 1 deletion internal/protoschema/jsonschema/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
func TestJSONSchemaGolden(t *testing.T) {
t.Parallel()
dirPath := filepath.FromSlash("../../testdata/jsonschema")
for _, testDesc := range golden.GetTestDescriptors() {
testDescs, err := golden.GetTestDescriptors("../../testdata")
require.NoError(t, err)
for _, testDesc := range testDescs {
for _, entry := range Generate(testDesc) {
// Serialize the JSON
data, err := json.MarshalIndent(entry, "", " ")
Expand Down
4 changes: 3 additions & 1 deletion internal/protoschema/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
func TestPubSubGolden(t *testing.T) {
t.Parallel()
dirPath := filepath.FromSlash("../../testdata/pubsub")
for _, testDesc := range golden.GetTestDescriptors() {
testDescs, err := golden.GetTestDescriptors("../../testdata")
require.NoError(t, err)
for _, testDesc := range testDescs {
filePath := filepath.Join(dirPath, string(testDesc.FullName()))
data, err := Generate(testDesc)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/codegenrequest/input.json

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a876f2b

Please sign in to comment.