Skip to content

Commit dbdceef

Browse files
committed
Add jsonschema:hide support
1 parent 94a121c commit dbdceef

File tree

6 files changed

+415
-42
lines changed

6 files changed

+415
-42
lines changed

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

+58-32
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

+3
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ message IgnoreField {
5555
// jsonschema:ignore
5656
int32 int32_field = 2;
5757
bool bool_field = 3;
58+
bytes bytes_field = 4; // jsonschema:hide
59+
// jsonschema:hide
60+
NestedReference nested_reference = 5;
5861
}

internal/protoschema/jsonschema/jsonschema.go

+34-9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ const (
3333
jsString = "string"
3434
)
3535

36+
type FieldVisibility int
37+
38+
const (
39+
FieldVisible FieldVisibility = iota
40+
FieldHide
41+
FieldIgnore
42+
)
43+
3644
// Generate generates a JSON schema for the given message descriptor.
3745
func Generate(input protoreflect.MessageDescriptor) map[protoreflect.FullName]map[string]interface{} {
3846
generator := &jsonSchemaGenerator{
@@ -75,20 +83,28 @@ func (p *jsonSchemaGenerator) generateDefault(result map[string]interface{}, des
7583
var patternProperties = make(map[string]interface{})
7684
for i := range desc.Fields().Len() {
7785
field := desc.Fields().Get(i)
78-
if p.shouldIgnoreField(field) {
86+
visibility := p.shouldIgnoreField(field)
87+
if visibility == FieldIgnore {
7988
continue
8089
}
8190

82-
// Generate the schema
91+
// Generate the schema.
8392
fieldSchema := p.generateField(field)
8493

8594
// TODO: Add an option to include custom alias.
8695
aliases := make([]string, 0, 1)
8796

88-
// TODO: Optionally make the json name the 'primary' name.
89-
properties[string(field.Name())] = fieldSchema
90-
if field.JSONName() != string(field.Name()) {
91-
aliases = append(aliases, field.JSONName())
97+
if visibility == FieldHide {
98+
aliases = append(aliases, string(field.Name()))
99+
if field.JSONName() != string(field.Name()) {
100+
aliases = append(aliases, field.JSONName())
101+
}
102+
} else {
103+
// TODO: Optionally make the json name the 'primary' name.
104+
properties[string(field.Name())] = fieldSchema
105+
if field.JSONName() != string(field.Name()) {
106+
aliases = append(aliases, field.JSONName())
107+
}
92108
}
93109

94110
if len(aliases) > 0 {
@@ -285,9 +301,18 @@ func (p *jsonSchemaGenerator) makeWktGenerators() map[protoreflect.FullName]func
285301
return result
286302
}
287303

288-
func (p *jsonSchemaGenerator) shouldIgnoreField(fdesc protoreflect.FieldDescriptor) bool {
304+
func (p *jsonSchemaGenerator) shouldIgnoreField(fdesc protoreflect.FieldDescriptor) FieldVisibility {
289305
const ignoreComment = "jsonschema:ignore"
306+
const hideComment = "jsonschema:hide"
290307
srcLoc := fdesc.ParentFile().SourceLocations().ByDescriptor(fdesc)
291-
return strings.Contains(srcLoc.LeadingComments, ignoreComment) ||
292-
strings.Contains(srcLoc.TrailingComments, ignoreComment)
308+
switch {
309+
case strings.Contains(srcLoc.LeadingComments, ignoreComment),
310+
strings.Contains(srcLoc.TrailingComments, ignoreComment):
311+
return FieldIgnore
312+
case strings.Contains(srcLoc.LeadingComments, hideComment),
313+
strings.Contains(srcLoc.TrailingComments, hideComment):
314+
return FieldHide
315+
default:
316+
return FieldVisible
317+
}
293318
}

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

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

0 commit comments

Comments
 (0)