Skip to content

Commit 34e18b3

Browse files
TrySoundkof
andauthored
fix: allow broken variables when validate updates (#4892)
Some templates have variables with missing scopeInstanceId on dialogs or something else from broken embed template. Here allow it in validation because migration would be more complex. --------- Co-authored-by: Oleg Isonen <[email protected]>
1 parent 250b525 commit 34e18b3

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

apps/builder/app/shared/data-variables.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ const traverseExpressions = ({
312312

313313
for (const dataSource of dataSources.values()) {
314314
if (
315-
instanceIds.has(dataSource.scopeInstanceId) &&
315+
instanceIds.has(dataSource.scopeInstanceId ?? "") &&
316316
dataSource.type === "resource"
317317
) {
318318
resourceIds.add(dataSource.resourceId);
@@ -440,15 +440,16 @@ export const deleteVariableMutable = (
440440
}
441441
const unsetNameById = new Map<DataSource["id"], DataSource["name"]>();
442442
unsetNameById.set(dataSource.id, dataSource.name);
443+
const startingInstanceId = dataSource.scopeInstanceId ?? "";
443444
const maskedIdByName = findMaskedVariablesByInstanceId({
444-
startingInstanceId: dataSource.scopeInstanceId,
445+
startingInstanceId,
445446
instances: data.instances,
446447
dataSources: data.dataSources,
447448
});
448449
// unset deleted variable in expressions
449450
traverseExpressions({
450451
...data,
451-
startingInstanceId: dataSource.scopeInstanceId,
452+
startingInstanceId,
452453
update: (expression) => {
453454
expression = unsetExpressionVariables({
454455
expression,

apps/builder/app/shared/instance-utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ export const deleteInstanceMutable = (
465465
}
466466
}
467467
for (const dataSource of dataSources.values()) {
468-
if (instanceIds.has(dataSource.scopeInstanceId)) {
468+
if (instanceIds.has(dataSource.scopeInstanceId ?? "")) {
469469
dataSources.delete(dataSource.id);
470470
if (dataSource.type === "resource") {
471471
resources.delete(dataSource.resourceId);
@@ -605,7 +605,7 @@ export const extractWebstudioFragment = (
605605
const fragmentResourceIds = new Set<Resource["id"]>();
606606
const unsetNameById = new Map<DataSource["id"], DataSource["name"]>();
607607
for (const dataSource of dataSources.values()) {
608-
if (fragmentInstanceIds.has(dataSource.scopeInstanceId)) {
608+
if (fragmentInstanceIds.has(dataSource.scopeInstanceId ?? "")) {
609609
fragmentDataSources.set(dataSource.id, dataSource);
610610
if (dataSource.type === "resource") {
611611
fragmentResourceIds.add(dataSource.resourceId);
@@ -866,7 +866,7 @@ export const insertWebstudioFragmentCopy = ({
866866
const usedResourceIds = new Set<Resource["id"]>();
867867
for (const dataSource of fragment.dataSources) {
868868
// insert only data sources within portal content
869-
if (instanceIds.has(dataSource.scopeInstanceId)) {
869+
if (instanceIds.has(dataSource.scopeInstanceId ?? "")) {
870870
dataSources.set(dataSource.id, dataSource);
871871
if (dataSource.type === "resource") {
872872
usedResourceIds.add(dataSource.resourceId);
@@ -956,7 +956,7 @@ export const insertWebstudioFragmentCopy = ({
956956
}
957957
const newResourceIds = new Map<Resource["id"], Resource["id"]>();
958958
for (let dataSource of fragment.dataSources) {
959-
const { scopeInstanceId } = dataSource;
959+
const scopeInstanceId = dataSource.scopeInstanceId ?? "";
960960
if (scopeInstanceId === ROOT_INSTANCE_ID) {
961961
// add global variable only if not exist already
962962
if (

packages/sdk/src/schema/data-sources.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ export const DataSource = z.union([
3030
z.object({
3131
type: z.literal("variable"),
3232
id: DataSourceId,
33-
scopeInstanceId: z.string(),
33+
// The instance should always be specified for variables,
34+
// however, there was a bug in the embed template
35+
// which produced variables without an instance
36+
// and these variables will fail validation
37+
// if we make it required
38+
scopeInstanceId: z.string().optional(),
3439
name: z.string(),
3540
value: DataSourceVariableValue,
3641
}),
3742
z.object({
3843
type: z.literal("parameter"),
3944
id: DataSourceId,
40-
scopeInstanceId: z.string(),
45+
scopeInstanceId: z.string().optional(),
4146
name: z.string(),
4247
}),
4348
z.object({
4449
type: z.literal("resource"),
4550
id: DataSourceId,
46-
scopeInstanceId: z.string(),
51+
scopeInstanceId: z.string().optional(),
4752
name: z.string(),
4853
resourceId: z.string(),
4954
}),

0 commit comments

Comments
 (0)