Skip to content

Commit dd3a73b

Browse files
authored
Merge pull request #538 from authzed/537-fix-warning-output
Fix regression in warning output
2 parents 0f1f661 + 89742e0 commit dd3a73b

5 files changed

Lines changed: 74 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
schema: |
2+
definition user {}
3+
4+
definition organization {}
5+
6+
definition platform {}
7+
8+
definition resource {
9+
/** platform is the platform to which the resource belongs */
10+
relation platform: platform
11+
12+
/**
13+
* organization is the organization to which the resource belongs
14+
*/
15+
relation organization: organization
16+
17+
/** admin is a user that can administer the resource */
18+
relation admin: user
19+
20+
/** viewer is a read-only viewer of the resource */
21+
relation viewer: user
22+
23+
/** can_admin allows a user to administer the resource */
24+
permission can_admin = admin
25+
26+
/** delete_resource allows a user to delete the resource. */
27+
permission delete_resource = can_admin
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
definition user {}
2+
3+
definition organization {}
4+
5+
definition platform {}
6+
7+
definition resource {
8+
/** platform is the platform to which the resource belongs */
9+
relation platform: platform
10+
11+
/**
12+
* organization is the organization to which the resource belongs
13+
*/
14+
relation organization: organization
15+
16+
/** admin is a user that can administer the resource */
17+
relation admin: user
18+
19+
/** viewer is a read-only viewer of the resource */
20+
relation viewer: user
21+
22+
/** can_admin allows a user to administer the resource */
23+
permission can_admin = admin
24+
25+
/** delete_resource allows a user to delete the resource. */
26+
permission delete_resource = can_admin
27+
}

internal/cmd/validate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
150150
}
151151

152152
var parsed validationfile.ValidationFile
153+
// the decoder is also where compilation happens.
153154
validateContents, isOnlySchema, err := decoder(&parsed)
154155
standardErrors, composableErrs, otherErrs := classifyErrors(err)
155156

@@ -208,6 +209,8 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
208209
return "", false, err
209210
}
210211
if devErrs != nil {
212+
// Calculate the schema offset, used for outputting errors and warnings
213+
// and having them point to the right place regardless of zed vs yaml
211214
schemaOffset := parsed.Schema.SourcePosition.LineNumber
212215
if isOnlySchema {
213216
schemaOffset = 0

internal/cmd/validate_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
215215
filepath.Join("validate-test", "missing-relation.zed"),
216216
},
217217
expectNonZeroStatusCode: true,
218-
expectStr: "error: parse error in `write`, line 2, column 20: relation/permission `write` not found under definition `test` \n" +
218+
expectStr: "error: parse error in `write`, line 2, column 21: relation/permission `write` not found under definition `test` \n" +
219219
" 1 | definition test {\n" +
220220
" 2 > permission view = write\n" +
221221
" > ^~~~~\n " +
@@ -297,6 +297,18 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
297297
complete - 0 relationships loaded, 0 assertions run, 0 expected relations validated
298298
`,
299299
},
300+
`warnings_point_at_correct_line_in_zed`: {
301+
files: []string{
302+
filepath.Join("validate-test", "warnings-point-at-right-line.zed"),
303+
},
304+
expectStr: "warning: Permission \"delete_resource\" references parent type \"resource\" in its name; it is recommended to drop the suffix (relation-name-references-parent)\n 23 | permission can_admin = admin\n 24 | \n 25 | /** delete_resource allows a user to delete the resource. */\n 26 > permission delete_resource = can_admin\n > ^~~~~~~~~~~~~~~\n 27 | }\n 28 | \n\ncomplete - 0 relationships loaded, 0 assertions run, 0 expected relations validated\n",
305+
},
306+
`warnings_point_at_correct_line_in_yaml`: {
307+
files: []string{
308+
filepath.Join("validate-test", "warnings-point-at-right-line.yaml"),
309+
},
310+
expectStr: "warning: Permission \"delete_resource\" references parent type \"resource\" in its name; it is recommended to drop the suffix (relation-name-references-parent)\n 23 | /** can_admin allows a user to administer the resource */\n 24 | permission can_admin = admin\n 25 | \n 26 > /** delete_resource allows a user to delete the resource. */\n > ^~~~~~~~~~~~~~~\n 27 | permission delete_resource = can_admin\n 28 | }\n\ncomplete - 0 relationships loaded, 0 assertions run, 0 expected relations validated\n",
311+
},
300312
}
301313

302314
for name, tc := range testCases {
@@ -306,7 +318,6 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
306318
require := require.New(t)
307319
cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t,
308320
zedtesting.StringFlag{FlagName: "schema-type", FlagValue: tc.schemaTypeFlag},
309-
zedtesting.BoolFlag{FlagName: "force-color", FlagValue: false},
310321
zedtesting.IntFlag{FlagName: "batch-size", FlagValue: 100},
311322
zedtesting.IntFlag{FlagName: "workers", FlagValue: 1},
312323
zedtesting.BoolFlag{FlagName: "fail-on-warn", FlagValue: false},

internal/decode/decoder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ func compileSchemaFromData(filename, schemaString string, out interface{}) error
200200
SchemaString: schemaString,
201201
}, composable.AllowUnprefixedObjectType(), composable.SourceFolder(inputSourceFolder))
202202

203-
if composableCompileErr == nil {
203+
// We'll only attempt to generate the composable schema string if we don't already
204+
// have one from standard schema compilation
205+
if composableCompileErr == nil && vfile.Schema.Schema == "" {
204206
compiledSchemaString, _, err := generator.GenerateSchema(composableCompiled.OrderedDefinitions)
205207
if err != nil {
206208
return fmt.Errorf("could not generate string schema: %w", err)

0 commit comments

Comments
 (0)