Skip to content

Commit

Permalink
Fix empty object internal error (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekrekr authored Jun 26, 2024
1 parent 0634d3a commit e9238a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
27 changes: 20 additions & 7 deletions common/protos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export function verifyObjectMatchesProto<Proto>(
// Empty arrays are assigned to empty proto array fields by ProtobufJS.
return;
}
if (!presentValue) {
throw ReferenceError(
`Unexpected empty value for "${presentKey}".` +
maybeGetDocsLinkPrefix(errorBehaviour, protoType)
);
}
if (typeof presentValue === "object" && Object.keys(presentValue).length === 0) {
// Empty objects are assigned to empty object fields by ProtobufJS.
return;
Expand All @@ -70,13 +76,7 @@ export function verifyObjectMatchesProto<Proto>(
throw ReferenceError(
`Unexpected property "${presentKey}", or property value type of ` +
`"${typeof presentValue}" is incorrect.` +
(errorBehaviour === VerifyProtoErrorBehaviour.SHOW_DOCS_LINK
? ` See ${CONFIGS_PROTO_DOCUMENTATION_URL}#${protoType
.getTypeUrl("")
// Clean up the proto type into its URL form.
.replace(/\./g, "-")
.replace(/\//, "")} for allowed properties.`
: "")
maybeGetDocsLinkPrefix(errorBehaviour, protoType)
);
}
if (typeof presentValue === "object") {
Expand All @@ -89,6 +89,19 @@ export function verifyObjectMatchesProto<Proto>(
return proto;
}

function maybeGetDocsLinkPrefix<Proto>(
errorBehaviour: VerifyProtoErrorBehaviour,
protoType: IProtoClass<any, Proto>
) {
return errorBehaviour === VerifyProtoErrorBehaviour.SHOW_DOCS_LINK
? ` See ${CONFIGS_PROTO_DOCUMENTATION_URL}#${protoType
.getTypeUrl("")
// Clean up the proto type into its URL form.
.replace(/\./g, "-")
.replace(/\//, "")} for allowed properties.`
: "";
}

export function encode64<IProto, Proto>(
protoType: IProtoClass<IProto, Proto>,
value: IProto | Proto = {} as IProto
Expand Down
18 changes: 18 additions & 0 deletions core/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,24 @@ actions:
);
});

test(`fails when empty objects are given`, () => {
const projectDir = tmpDirFixture.createNewTmpDir();
fs.writeFileSync(
path.join(projectDir, "workflow_settings.yaml"),
VALID_WORKFLOW_SETTINGS_YAML
);
fs.mkdirSync(path.join(projectDir, "definitions"));
fs.writeFileSync(
path.join(projectDir, "definitions/actions.yaml"),
`
actions:`
);

expect(() => runMainInVm(coreExecutionRequestFromPath(projectDir))).to.throw(
`Unexpected empty value for "actions". See https://dataform-co.github.io/dataform/docs/configs-reference#dataform-ActionConfigs for allowed properties.`
);
});

test(`filenames with non-UTF8 characters are valid`, () => {
const projectDir = tmpDirFixture.createNewTmpDir();
fs.writeFileSync(
Expand Down

0 comments on commit e9238a4

Please sign in to comment.