Skip to content

Commit 29b0ff3

Browse files
committed
JSON Schema: Ignore fields via comment
1 parent 9c12400 commit 29b0ff3

25 files changed

+217
-39
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ test: build $(BIN)/jv ## Run unit tests
3535
golden: generate
3636
rm -rf internal/testdata/pubsub
3737
rm -rf internal/testdata/jsonschema
38+
buf build ./internal/proto -o -#format=json > ./internal/testdata/codegenrequest/input.json
3839
go run internal/cmd/pubsub-generate-testdata/main.go internal/testdata/pubsub
3940
go run internal/cmd/jsonschema-generate-testdata/main.go internal/testdata/jsonschema
40-
buf build ./internal/proto --exclude-source-info -o -#format=json > ./internal/testdata/codegenrequest/input.json
4141

4242
.PHONY: build
4343
build: generate ## Build all packages

internal/cmd/jsonschema-generate-testdata/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ func run() error {
4141
}
4242
outputDir := os.Args[1]
4343

44-
for _, testDesc := range golden.GetTestDescriptors() {
44+
testDescs, err := golden.GetTestDescriptors("./internal/testdata")
45+
if err != nil {
46+
return err
47+
}
48+
for _, testDesc := range testDescs {
4549
// Generate the JSON schema
4650
result := jsonschema.Generate(testDesc)
4751

internal/cmd/pubsub-generate-testdata/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func run() error {
5151
}
5252

5353
// Generate the testdata
54-
for _, testDesc := range golden.GetTestDescriptors() {
54+
testDescs, err := golden.GetTestDescriptors("./internal/testdata")
55+
if err != nil {
56+
return err
57+
}
58+
for _, testDesc := range testDescs {
5559
filePath := filepath.Join(dirPath, fmt.Sprintf("%s.%s", testDesc.FullName(), pubsub.FileExtension))
5660
data, err := pubsub.Generate(testDesc)
5761
if err != nil {

internal/gen/proto/buf/protoschema/test/v1/test_cases.pb.go

+107-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/gen/proto/bufext/cel/expr/conformance/proto3/test_all_types.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/proto/buf/protoschema/test/v1/test_cases.proto

+7
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ message CustomOptions {
4949
}];
5050
}
5151
}
52+
53+
message IgnoreField {
54+
string string_field = 1; // jsonschema:ignore
55+
// jsonschema:ignore
56+
int32 int32_field = 2;
57+
bool bool_field = 3;
58+
}

internal/protoschema/golden/golden.go

+37-8
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,50 @@ import (
1818
"fmt"
1919
"io"
2020
"os"
21+
"path/filepath"
2122

22-
testv1 "github.com/bufbuild/protoschema-plugins/internal/gen/proto/buf/protoschema/test/v1"
23-
"github.com/bufbuild/protoschema-plugins/internal/gen/proto/bufext/cel/expr/conformance/proto3"
23+
"google.golang.org/protobuf/encoding/protojson"
24+
"google.golang.org/protobuf/reflect/protodesc"
2425
"google.golang.org/protobuf/reflect/protoreflect"
26+
"google.golang.org/protobuf/types/descriptorpb"
27+
"google.golang.org/protobuf/types/dynamicpb"
2528
)
2629

2730
// GetTestDescriptors returns the test descriptors that were generated from the ./internal/proto
2831
// directory.
29-
func GetTestDescriptors() []protoreflect.MessageDescriptor {
30-
return []protoreflect.MessageDescriptor{
31-
(&proto3.TestAllTypes{}).ProtoReflect().Descriptor(),
32-
(&proto3.NestedTestAllTypes{}).ProtoReflect().Descriptor(),
33-
(&testv1.NestedReference{}).ProtoReflect().Descriptor(),
34-
(&testv1.CustomOptions{}).ProtoReflect().Descriptor(),
32+
func GetTestDescriptors(testdataPath string) ([]protoreflect.MessageDescriptor, error) {
33+
inputPath := filepath.Join(filepath.FromSlash(testdataPath), "codegenrequest", "input.json")
34+
input, err := os.ReadFile(inputPath)
35+
if err != nil {
36+
return nil, fmt.Errorf("failed to open input file descritpor set at %q: %w", inputPath, err)
37+
}
38+
fdset := &descriptorpb.FileDescriptorSet{}
39+
if err = (&protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(input, fdset); err != nil {
40+
return nil, fmt.Errorf("failed to parse file descriptor set at %q: %w", inputPath, err)
41+
}
42+
files, err := protodesc.NewFiles(fdset)
43+
if err != nil {
44+
return nil, fmt.Errorf("failed to link file descriptor set at %q: %w", inputPath, err)
45+
}
46+
types := dynamicpb.NewTypes(files)
47+
48+
fqns := []protoreflect.FullName{
49+
"bufext.cel.expr.conformance.proto3.TestAllTypes",
50+
"bufext.cel.expr.conformance.proto3.NestedTestAllTypes",
51+
"buf.protoschema.test.v1.NestedReference",
52+
"buf.protoschema.test.v1.CustomOptions",
53+
"buf.protoschema.test.v1.IgnoreField",
54+
}
55+
56+
msgs := make([]protoreflect.MessageDescriptor, len(fqns))
57+
for i, fqn := range fqns {
58+
mType, err := types.FindMessageByName(fqn)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to find message %q: %w", fqn, err)
61+
}
62+
msgs[i] = mType.Descriptor()
3563
}
64+
return msgs, nil
3665
}
3766

3867
// CheckGolden checks the golden file exists and matches the given data.

internal/protoschema/jsonschema/jsonschema.go

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (p *jsonSchemaGenerator) generateDefault(result map[string]interface{}, des
7474
var properties = make(map[string]interface{})
7575
for i := range desc.Fields().Len() {
7676
field := desc.Fields().Get(i)
77+
if p.shouldIgnoreField(field) {
78+
continue
79+
}
7780
name := string(field.Name())
7881
properties[name] = p.generateField(field)
7982
}
@@ -262,3 +265,10 @@ func (p *jsonSchemaGenerator) makeWktGenerators() map[protoreflect.FullName]func
262265
result["google.protobuf.UInt64Value"] = p.generateWrapperValidation
263266
return result
264267
}
268+
269+
func (p *jsonSchemaGenerator) shouldIgnoreField(fdesc protoreflect.FieldDescriptor) bool {
270+
const ignoreComment = "jsonschema:ignore"
271+
srcLoc := fdesc.ParentFile().SourceLocations().ByDescriptor(fdesc)
272+
return strings.Contains(srcLoc.LeadingComments, ignoreComment) ||
273+
strings.Contains(srcLoc.TrailingComments, ignoreComment)
274+
}

internal/protoschema/jsonschema/jsonschema_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
func TestJSONSchemaGolden(t *testing.T) {
2727
t.Parallel()
2828
dirPath := filepath.FromSlash("../../testdata/jsonschema")
29-
for _, testDesc := range golden.GetTestDescriptors() {
29+
testDescs, err := golden.GetTestDescriptors("../../testdata")
30+
require.NoError(t, err)
31+
for _, testDesc := range testDescs {
3032
for _, entry := range Generate(testDesc) {
3133
// Serialize the JSON
3234
data, err := json.MarshalIndent(entry, "", " ")

internal/protoschema/pubsub/pubsub_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
func TestPubSubGolden(t *testing.T) {
2727
t.Parallel()
2828
dirPath := filepath.FromSlash("../../testdata/pubsub")
29-
for _, testDesc := range golden.GetTestDescriptors() {
29+
testDescs, err := golden.GetTestDescriptors("../../testdata")
30+
require.NoError(t, err)
31+
for _, testDesc := range testDescs {
3032
filePath := filepath.Join(dirPath, string(testDesc.FullName()))
3133
data, err := Generate(testDesc)
3234
require.NoError(t, err)

internal/testdata/codegenrequest/input.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/testdata/jsonschema/buf.protoschema.test.v1.IgnoreField.schema.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/testdata/jsonschema/bufext.cel.expr.conformance.proto3.NestedTestAllTypes.schema.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/testdata/jsonschema/bufext.cel.expr.conformance.proto3.TestAllTypes.NestedMessage.schema.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)