-
Notifications
You must be signed in to change notification settings - Fork 323
fix(api): align SchemaDefinition OpenAPI names with HTTP JSON #3098
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
Open
BetterAndBetterII
wants to merge
1
commit into
Permify:master
Choose a base branch
from
BetterAndBetterII:fix/schema-read-json-names
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package servers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
| "google.golang.org/protobuf/encoding/protojson" | ||
|
|
||
| v1 "github.com/Permify/permify/pkg/pb/base/v1" | ||
| ) | ||
|
|
||
| func TestSchemaReadHTTPJSONNamesMatchOpenAPI(t *testing.T) { | ||
| resp := &v1.SchemaReadResponse{ | ||
| Schema: &v1.SchemaDefinition{ | ||
| EntityDefinitions: map[string]*v1.EntityDefinition{ | ||
| "user": {Name: "user"}, | ||
| }, | ||
| RuleDefinitions: map[string]*v1.RuleDefinition{ | ||
| "is_weekday": {Name: "is_weekday"}, | ||
| }, | ||
| References: map[string]v1.SchemaDefinition_Reference{ | ||
| "user": v1.SchemaDefinition_REFERENCE_ENTITY, | ||
| "is_weekday": v1.SchemaDefinition_REFERENCE_RULE, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| marshaler := &gwruntime.JSONPb{ | ||
| MarshalOptions: protojson.MarshalOptions{ | ||
| UseProtoNames: true, | ||
| EmitUnpopulated: true, | ||
| }, | ||
| UnmarshalOptions: protojson.UnmarshalOptions{ | ||
| DiscardUnknown: true, | ||
| }, | ||
| } | ||
|
|
||
| raw, err := marshaler.Marshal(resp) | ||
| if err != nil { | ||
| t.Fatalf("marshal schema read response: %v", err) | ||
| } | ||
|
|
||
| var body map[string]json.RawMessage | ||
| if err := json.Unmarshal(raw, &body); err != nil { | ||
| t.Fatalf("decode marshaled response: %v", err) | ||
| } | ||
|
|
||
| schemaRaw, ok := body["schema"] | ||
| if !ok { | ||
| t.Fatalf("HTTP schema read JSON missing schema object: %s", raw) | ||
| } | ||
|
|
||
| var schema map[string]json.RawMessage | ||
| if err := json.Unmarshal(schemaRaw, &schema); err != nil { | ||
| t.Fatalf("decode schema object: %v", err) | ||
| } | ||
|
|
||
| for _, name := range []string{"entity_definitions", "rule_definitions"} { | ||
| if _, ok := schema[name]; !ok { | ||
| t.Errorf("HTTP schema JSON missing %q; keys=%v body=%s", name, jsonKeys(schema), raw) | ||
| } | ||
| } | ||
| for _, name := range []string{"entityDefinitions", "ruleDefinitions"} { | ||
| if _, ok := schema[name]; ok { | ||
| t.Errorf("HTTP schema JSON unexpectedly used camelCase %q; body=%s", name, raw) | ||
| } | ||
| } | ||
|
|
||
| root := findRepoRoot(t) | ||
| specs := []struct { | ||
| path string | ||
| props func(map[string]any) map[string]any | ||
| }{ | ||
| { | ||
| path: filepath.Join(root, "docs/api-reference/openapi.json"), | ||
| props: func(doc map[string]any) map[string]any { | ||
| return nestedMap(doc, "components", "schemas", "SchemaDefinition", "properties") | ||
| }, | ||
| }, | ||
| { | ||
| path: filepath.Join(root, "docs/api-reference/apidocs.swagger.json"), | ||
| props: func(doc map[string]any) map[string]any { | ||
| return nestedMap(doc, "definitions", "SchemaDefinition", "properties") | ||
| }, | ||
| }, | ||
| { | ||
| path: filepath.Join(root, "docs/api-reference/openapiv2/apidocs.swagger.json"), | ||
| props: func(doc map[string]any) map[string]any { | ||
| return nestedMap(doc, "definitions", "SchemaDefinition", "properties") | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, spec := range specs { | ||
| t.Run(spec.path, func(t *testing.T) { | ||
| doc := readJSONObject(t, spec.path) | ||
| props := spec.props(doc) | ||
| if props == nil { | ||
| t.Fatalf("SchemaDefinition properties missing in %s", spec.path) | ||
| } | ||
| for _, name := range []string{"entity_definitions", "rule_definitions"} { | ||
| if _, ok := props[name]; !ok { | ||
| t.Errorf("%s SchemaDefinition missing %q; properties=%v", spec.path, name, jsonKeys(props)) | ||
| } | ||
| } | ||
| for _, name := range []string{"entityDefinitions", "ruleDefinitions"} { | ||
| if _, ok := props[name]; ok { | ||
| t.Errorf("%s SchemaDefinition still documents camelCase %q", spec.path, name) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func findRepoRoot(t *testing.T) string { | ||
| t.Helper() | ||
| dir, err := os.Getwd() | ||
| if err != nil { | ||
| t.Fatalf("getwd: %v", err) | ||
| } | ||
| for { | ||
| if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { | ||
| return dir | ||
| } | ||
| parent := filepath.Dir(dir) | ||
| if parent == dir { | ||
| t.Fatal("go.mod not found") | ||
| } | ||
| dir = parent | ||
| } | ||
| } | ||
|
|
||
| func readJSONObject(t *testing.T, path string) map[string]any { | ||
| t.Helper() | ||
| raw, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatalf("read %s: %v", path, err) | ||
| } | ||
| var doc map[string]any | ||
| if err := json.Unmarshal(raw, &doc); err != nil { | ||
| t.Fatalf("decode %s: %v", path, err) | ||
| } | ||
| return doc | ||
| } | ||
|
|
||
| func nestedMap(doc map[string]any, keys ...string) map[string]any { | ||
| cur := any(doc) | ||
| for _, key := range keys { | ||
| obj, ok := cur.(map[string]any) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| cur, ok = obj[key] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| } | ||
| props, _ := cur.(map[string]any) | ||
| return props | ||
| } | ||
|
|
||
| func jsonKeys[V any](m map[string]V) []string { | ||
| keys := make([]string, 0, len(m)) | ||
| for k := range m { | ||
| keys = append(keys, k) | ||
| } | ||
| return keys | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: Permify/permify
Length of output: 2636
🏁 Script executed:
Repository: Permify/permify
Length of output: 50372
🏁 Script executed:
Repository: Permify/permify
Length of output: 5023
Add a default
protojsonassertion forjson_name.UseProtoNames: truemakes the test emit proto field names, so it does not validate the explicitjson_namevalues. Add a separate marshal assertion withoutUseProtoNames, then regeneratepkg/pb/base/v1/base.pb.goif it emits camelCase.🤖 Prompt for AI Agents