Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validation for the JSON field name #16

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions internal/protoschema/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,35 @@ func (p *jsonSchemaGenerator) generateDefault(result map[string]interface{}, des
result["type"] = jsObject
p.setDescription(desc, result)
var properties = make(map[string]interface{})
var patternProperties = 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)

// Generate the schema
fieldSchema := p.generateField(field)

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

// TODO: Optionally make the json name the 'primary' name.
properties[string(field.Name())] = fieldSchema
if field.JSONName() != string(field.Name()) {
aliases = append(aliases, field.JSONName())
}

if len(aliases) > 0 {
pattern := "^(" + strings.Join(aliases, "|") + ")$"
patternProperties[pattern] = fieldSchema
}
}
result["properties"] = properties
result["additionalProperties"] = false
if len(patternProperties) > 0 {
result["patternProperties"] = patternProperties
}
}

func (p *jsonSchemaGenerator) setDescription(desc protoreflect.Descriptor, result map[string]interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/codegenrequest/input.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion internal/testdata/jsonschema-doc/test.TestAllTypes.yaml

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.

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

Loading