Skip to content

Commit

Permalink
v0.9.0: Fix non-deterministic schema hash (#172)
Browse files Browse the repository at this point in the history
* Fix non-deterministic schema hash

Schema hash was generated from schema.JSON(nil) including Imports
pointing at absolute paths. The CI generated different hash compared
to my development setup when there were RIDL imports involved.

1. Can we simply disable the Imports field from Schema JSON?
2. Do we need to keep schema.Imports around at all?

* Remove schema.Imports
  • Loading branch information
VojtechVitek committed Dec 24, 2022
1 parent 945fe4c commit 5e6d1b8
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 139 deletions.
4 changes: 2 additions & 2 deletions _examples/golang-basics/example.gen.go

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

4 changes: 2 additions & 2 deletions _examples/golang-nodejs/client/client.gen.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// example v0.0.1 33aa93c6d912046df938c7f94cef36d3a30679fa
// example v0.0.1 d4a4a8f6a7aeaba88f2c21b4c95ca7e837277901
// --
// Code generated by webrpc-gen@v0.8.x-dev with javascript generator. DO NOT EDIT.
//
Expand All @@ -11,7 +11,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = " v0.0.1"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "33aa93c6d912046df938c7f94cef36d3a30679fa"
export const WebRPCSchemaHash = "d4a4a8f6a7aeaba88f2c21b4c95ca7e837277901"

//
// Types
Expand Down
4 changes: 2 additions & 2 deletions _examples/golang-nodejs/server/server.gen.go

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

4 changes: 2 additions & 2 deletions _examples/hello-webrpc-ts/server/hello_api.gen.go

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

4 changes: 2 additions & 2 deletions _examples/hello-webrpc-ts/webapp/src/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// hello-webrpc v1.0.0 5ace0c3aa305e464d6a2c180f43e8876be34e155
// hello-webrpc v1.0.0 ee3e93a9cf07c3e432f94ddb6e325bd2f5e2a622
// --
// Code generated by webrpc-gen@v0.8.x-dev with typescript generator. DO NOT EDIT.
//
Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "5ace0c3aa305e464d6a2c180f43e8876be34e155"
export const WebRPCSchemaHash = "ee3e93a9cf07c3e432f94ddb6e325bd2f5e2a622"

//
// Types
Expand Down
4 changes: 2 additions & 2 deletions _examples/hello-webrpc/server/hello_api.gen.go

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

4 changes: 2 additions & 2 deletions _examples/hello-webrpc/webapp/client.gen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// hello-webrpc v1.0.0 d12378d7d88e036c2e5f779db475e7144b638b26
// hello-webrpc v1.0.0 8ea9e92729e91a1619e267a7154f9518f8f19cec
// --
// Code generated by webrpc-gen@v0.8.x-dev with javascript generator. DO NOT EDIT.
//
Expand All @@ -11,7 +11,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "d12378d7d88e036c2e5f779db475e7144b638b26"
export const WebRPCSchemaHash = "8ea9e92729e91a1619e267a7154f9518f8f19cec"

//
// Types
Expand Down
4 changes: 2 additions & 2 deletions _examples/node-ts/server/server.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// node-ts v1.0.0 ad568d12a684eb48ae2b38f98a2933cd65f7a4fc
// node-ts v1.0.0 ffca8190fb45b90618e2a73fe505d63213f39795
// --
// Code generated by webrpc-gen@v0.8.x-dev with typescript generator. DO NOT EDIT.
//
Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "ad568d12a684eb48ae2b38f98a2933cd65f7a4fc"
export const WebRPCSchemaHash = "ffca8190fb45b90618e2a73fe505d63213f39795"

//
// Types
Expand Down
4 changes: 2 additions & 2 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// node-ts v1.0.0 ad568d12a684eb48ae2b38f98a2933cd65f7a4fc
// node-ts v1.0.0 ffca8190fb45b90618e2a73fe505d63213f39795
// --
// Code generated by webrpc-gen@v0.8.x-dev with typescript generator. DO NOT EDIT.
//
Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "ad568d12a684eb48ae2b38f98a2933cd65f7a4fc"
export const WebRPCSchemaHash = "ffca8190fb45b90618e2a73fe505d63213f39795"

//
// Types
Expand Down
13 changes: 0 additions & 13 deletions schema/ridl/_example/example1-golden.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
{
"imports": [
{
"members": [],
"path": "example1-definitions.ridl"
},
{
"members": [
"Empty",
"GetUserRequest"
],
"path": "example1-partials.ridl"
}
],
"messages": [
{
"fields": [
Expand Down
22 changes: 8 additions & 14 deletions schema/ridl/ridl.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (p *Parser) parse() (*schema.WebRPCSchema, error) {
}

s := &schema.WebRPCSchema{
Imports: []*schema.Import{},
Messages: []*schema.Message{},
Services: []*schema.Service{},
}
Expand Down Expand Up @@ -114,31 +113,26 @@ func (p *Parser) parse() (*schema.WebRPCSchema, error) {
for _, line := range q.root.Imports() {
importPath := filepath.Join(filepath.Dir(p.reader.File), line.Path().String())

importDef := &schema.Import{
Path: importPath,
Members: []string{},
}
for _, member := range line.Members() {
importDef.Members = append(importDef.Members, member.String())
}

imported, err := p.importRIDLFile(importDef.Path)
imported, err := p.importRIDLFile(importPath)
if err != nil {
return nil, p.trace(err, line.Path())
}

members := []string{}
for _, member := range line.Members() {
members = append(members, member.String())
}

for i := range imported.Messages {
if isImportAllowed(string(imported.Messages[i].Name), importDef.Members) {
if isImportAllowed(string(imported.Messages[i].Name), members) {
s.Messages = append(s.Messages, imported.Messages[i])
}
}
for i := range imported.Services {
if isImportAllowed(string(imported.Services[i].Name), importDef.Members) {
if isImportAllowed(string(imported.Services[i].Name), members) {
s.Services = append(s.Services, imported.Services[i])
}
}

s.Imports = append(s.Imports, importDef)
}

// pushing enums (1st pass)
Expand Down
84 changes: 0 additions & 84 deletions schema/ridl/ridl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func TestRIDLImport(t *testing.T) {
assert.Equal(t, "hello-webrpc", s.SchemaName)
assert.Equal(t, "v0.1.1", s.SchemaVersion)

assert.Equal(t, "foo", s.Imports[0].Path)
assert.Equal(t, "bar", s.Imports[1].Path)
}

{
Expand All @@ -114,9 +112,6 @@ func TestRIDLImport(t *testing.T) {
assert.Equal(t, "v1", s.WebrpcVersion)
assert.Equal(t, "hello-webrpc", s.SchemaName)
assert.Equal(t, "v0.1.1", s.SchemaVersion)

assert.Equal(t, "foo1", s.Imports[0].Path)
assert.Equal(t, "bar2", s.Imports[1].Path)
}
}

Expand Down Expand Up @@ -426,85 +421,6 @@ func TestRIDLParse(t *testing.T) {
assert.NotZero(t, jout)
}

func TestRIDLTables(t *testing.T) {
enableMockImport()
defer disableMockImport()

table := []struct {
Input string
Output []byte
}{
{
// Whitespace bug
"webrpc = v1\n \nname = test\n \nversion=v1.1\n",
[]byte(`
{
"webrpc": "v1",
"name": "test",
"version": "v1.1",
"imports": [],
"messages": [],
"services": []
}
`),
},
{
"webrpc = v1\n \nname = test\n",
[]byte(`
{
"webrpc": "v1",
"name": "test",
"version": "",
"imports": [],
"messages": [],
"services": []
}
`),
},
{
`
webrpc = v1
name = hello-webrpc
version = v0.0.1
import
- ./blah.ridl
- ./abc.json
`,
[]byte(`
{
"webrpc": "v1",
"name": "hello-webrpc",
"version": "v0.0.1",
"imports": [
{
"path": "blah.ridl",
"members": []
},
{
"path": "abc.json",
"members": []
}
],
"messages": [],
"services": []
}
`),
},
}

for i := range table {
s, err := parseString(table[i].Input)
assert.NoError(t, err)

jout, err := s.ToJSON(true)
assert.NoError(t, err)

assert.JSONEq(t, compactJSON(table[i].Output), compactJSON([]byte(jout)), fmt.Sprintf("GOT:\n\n%s\n\nEXPECTING:\n\n%s\n\n", jout, string(table[i].Output)))
}
}

func TestRIDLImports(t *testing.T) {
os.Chdir("_example")

Expand Down
6 changes: 0 additions & 6 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ type WebRPCSchema struct {
SchemaName string `json:"name"`
SchemaVersion string `json:"version"`

Imports []*Import `json:"imports"`
Messages []*Message `json:"messages"`
Services []*Service `json:"services"`
}

type Import struct {
Path string `json:"path"`
Members []string `json:"members"`
}

// Validate validates the schema through the AST, intended to be called after
// the json has been unmarshalled
func (s *WebRPCSchema) Validate() error {
Expand Down
4 changes: 2 additions & 2 deletions tests/client/client.gen.go

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

4 changes: 2 additions & 2 deletions tests/server/server.gen.go

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

0 comments on commit 5e6d1b8

Please sign in to comment.