Skip to content

Commit

Permalink
fix: Fix pluginOptions passed as nil and add testcases and update docs (
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid authored Nov 8, 2024
1 parent 54ee33e commit be0b8dc
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ example:
- --jsonschema_opt=int64_as_string=false
```

### pretty_json_output
### preserve_proto_field_names
```
preserve_proto_field_names is used to determine if output json field names
should be identical to the proto field names.
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/0_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (m *Module) InitContext(c pgs.BuildContext) {

func (m *Module) Execute(targets map[string]pgs.File, packages map[string]pgs.Package) []pgs.Artifact {
// Phase: Middleend IntermediateSchemaGenerate
visitor := NewVisitor(m)
visitor := NewVisitor(m, m.pluginOptions)
for _, pkg := range packages {
m.CheckErr(pgs.Walk(visitor, pkg), fmt.Sprintf("failed to walk package %s", pkg.ProtoName().String()))
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/modules/1_middleend_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ type MiddleendVisitor struct {

var _ pgs.Visitor = (*MiddleendVisitor)(nil)

func NewVisitor(debugger pgs.DebuggerCommon) *MiddleendVisitor {
func NewVisitor(debugger pgs.DebuggerCommon, pluginOptions *proto.PluginOptions) *MiddleendVisitor {
v := &MiddleendVisitor{
debugger: debugger,
registry: jsonschema.NewRegistry(),
debugger: debugger,
registry: jsonschema.NewRegistry(),
pluginOptions: pluginOptions,
}
v.Visitor = pgs.PassThroughVisitor(v)
return v
Expand Down
97 changes: 95 additions & 2 deletions testdata/README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,129 @@
# TestCases

This directory contains test cases for the protoc-gen-jsonschema plugin.

# Make a new test case

1. Create a new directory in the `testdata/cases` directory.
2. Create `test.proto` file and `test.yaml` file in the new directory.
3. Fill the `test.proto` file with proper content.
4. Fill the `test.yaml` file with expected test result.

### test.yaml format

```yaml
name: "proto-json-name-ignore" # test case name, should be unique
description: "test for ignore proto json_name option" # description of the test case

inputFiles: ["test.proto"] # input proto file
inputParameters: # protoc-gen-jsonschema plugin options. Map<string, any> type
preserve_proto_field_names: true

expectResultFiles: ["test.schema.json"] # expected output schema file
expectResultIsNull: false # if true, expectResultFile is empty test will pass
expectFail: false # if true, generate schema fail test will pass

expectedBehaviorDescription: "" # not used
```
### Generate all test case results
```sh
cd hack
./generate_all_test_result.sh
```

### Generate single test case result

```sh
cd hack
./generate_test_result.sh <test case name>
```
# Test all cases
```sh
go test do_test.go
```
# Test single case
Add ad-hoc filter code to the test code
```go
46 for _, dir := range dirs {
47 if dir.Name() != "proto-json-name-ignore" {
48 continue
49 }
...
}
```
```sh
go test do_test.go
```
# Description of each test case
proto-oneof-single oneof가 잘 전환되는지 테스트
proto-oneof-multiple oneof가 잘 전환되는지 테스트
proto-number int32, int64등이 number로 잘 전환되는지 테스트
proto-string string가 string으로 잘 전환되는지 테스트, bytes가 base64 string으로 잘 전환되는지 테스트
proto-repeated-scala repeated가 array로 잘 전환되는지 테스트
proto-repeated-wellknown
proto-repeated-wellknown
proto-repeated-message-and-map
proto-optional optional이 있다면 required에서 빠지는지 테스트
proto-optional optional이 있다면 required에서 빠지는지 테스트
proto-message message가 object로 잘 전환되는지 테스트
proto-enum enum이 enum으로 잘 전환되는지 테스트
proto-json-name json_name이 잘 적용되는지 테스트
proto-json-name-ignore json_name이 무시되는지 테스트
proto-map map이 object로 잘 전환되는지 테스트, array 속성이 들어가면 안됨
proto-nested-message nested message가 잘 전환되는지 테스트
jsonschema-nullable nullable 속성이 있으면 oneof로 잘 전환되는지 테스트
jsonschema-number number validation keyword 테스트
jsonschema-string string validation keyword 테스트
jsonschema-array array validation keyword 테스트
jsonschema-object object validation keyword 테스트
[//]: # (jsonschema-enum enum validation keyword 테스트)
[//]: # (jsonschema-const const validation keyword 테스트)
plugin-entrypoint-options plugin entrypoint가 잘 작동하는지 테스트
plugin-output-options output 관련 옵션이 잘 작동하는지 테스트
plugin-draft-04 draft-04 스키마가 잘 작동하는지 테스트
plugin-draft-06 draft-06 스키마가 잘 작동하는지 테스트
plugin-draft-07 draft-07 스키마가 잘 작동하는지 테스트
plugin-draft-201909 draft-2019-09 스키마가 잘 작동하는지 테스트
plugin-draft-202012 draft-2020-12 스키마가 잘 작동하는지 테스트
wellknown-any google.protobuf.Any가 잘 전환되는지 테스트
wellknown-timestamp google.protobuf.Timestamp가 잘 전환되는지 테스트
wellknown-duration google.protobuf.Duration가 잘 전환되는지 테스트
wellknown-k8s-intorstring k8s.io.apimachinery.pkg.util.intstr.IntOrString가 잘 전환되는지 테스트
16 changes: 16 additions & 0 deletions testdata/cases/proto-json-name-ignore/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package tests;

import "jsonschema.proto";

option (pubg.jsonschema.file) = {
entrypoint_message : "Values"
};

message Values {
string foo_bar_baz = 1 [ json_name = "fooBarBaz" ];
string implicit_camel_case = 2;
string explicit_snake_case = 3 [ json_name = "explicit_snake_case" ];
string arbitrary_value = 4 [ json_name = "abc123" ];
}
42 changes: 42 additions & 0 deletions testdata/cases/proto-json-name-ignore/test.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/.tests.Values",
"$defs": {
".tests.Values": {
"properties": {
"foo_bar_baz": {
"$ref": "#/$defs/.tests.Values.foo_bar_baz"
},
"implicit_camel_case": {
"$ref": "#/$defs/.tests.Values.implicit_camel_case"
},
"explicit_snake_case": {
"$ref": "#/$defs/.tests.Values.explicit_snake_case"
},
"arbitrary_value": {
"$ref": "#/$defs/.tests.Values.arbitrary_value"
}
},
"type": "object",
"required": [
"foo_bar_baz",
"implicit_camel_case",
"explicit_snake_case",
"arbitrary_value"
]
},
".tests.Values.foo_bar_baz": {
"type": "string"
},
".tests.Values.implicit_camel_case": {
"type": "string"
},
".tests.Values.explicit_snake_case": {
"type": "string"
},
".tests.Values.arbitrary_value": {
"type": "string"
}
},
"type": "object"
}
12 changes: 12 additions & 0 deletions testdata/cases/proto-json-name-ignore/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "proto-json-name-ignore"
description: "test for ignore proto json_name option"

inputFiles: ["test.proto"]
inputParameters:
preserve_proto_field_names: true

expectResultFiles: ["test.schema.json"]
expectResultIsNull: false
expectFail: false

expectedBehaviorDescription: ""
2 changes: 1 addition & 1 deletion testdata/cases/proto-json-name/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "proto-json-name"
description: ""
description: "test for respect proto json_name option"

inputFiles: ["test.proto"]
inputParameters: {}
Expand Down
2 changes: 1 addition & 1 deletion testdata/hack/generate_test_result.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function generateProtoFile() {

command="protoc \
--plugin=protoc-gen-jsonschema=./testdata/hack/protoc-gen-jsonschema \
--jsonschema_out=./testdata/cases/$proto_dir " # 초기 명령어 추가
--jsonschema_out=. " # 초기 명령어 추가

# test.yaml 파일의 존재 여부 확인
if [[ -f "./testdata/cases/$proto_dir/test.yaml" ]]; then
Expand Down

0 comments on commit be0b8dc

Please sign in to comment.