Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {loadLocalExtensionsSpecifications} from './load-specifications.js'
import {configWithoutFirstClassFields, createContractBasedModuleSpecification} from './specification.js'
import {
configWithoutFirstClassFields,
createConfigExtensionSpecification,
createContractBasedModuleSpecification,
} from './specification.js'
import {BaseSchemaWithoutHandle} from './schemas.js'
import {AppSchema} from '../app/app.js'
import {describe, test, expect, beforeAll} from 'vitest'
import {zod} from '@shopify/cli-kit/node/schema'

// If the AppSchema is not instanced, the dynamic loading of loadLocalExtensionsSpecifications is not working
beforeAll(() => {
Expand Down Expand Up @@ -48,6 +54,70 @@ describe('createContractBasedModuleSpecification', () => {
})
})

describe('createConfigExtensionSpecification', () => {
const TestSchema = BaseSchemaWithoutHandle.extend({
name: zod.string().optional(),
handle: zod.string().optional(),
})

test('derives transforms from transformConfig when direct params are not provided', () => {
const spec = createConfigExtensionSpecification({
identifier: 'test-module',
schema: TestSchema,
transformConfig: {server_name: 'name'},
})

// Forward transform maps TOML field names to server field names
const transformed = spec.transformLocalToRemote!({name: 'My App'}, {} as any)
expect(transformed).toEqual({server_name: 'My App'})

// Reverse transform maps server field names back to TOML
const reversed = spec.transformRemoteToLocal!({server_name: 'My App'})
expect(reversed).toEqual({name: 'My App'})

expect(spec.experience).toBe('configuration')
expect(spec.uidStrategy).toBe('single')
})

test('uses deployConfig and transformRemoteToLocal directly when provided without transformConfig', () => {
const spec = createConfigExtensionSpecification({
identifier: 'test-module',
schema: TestSchema,
deployConfig: async (config) => ({name: (config as any).name}),
transformRemoteToLocal: (content: object) => ({
name: (content as any).server_name,
}),
})

// No forward transform — deployConfig handles the deploy path instead
expect(spec.transformLocalToRemote).toBeUndefined()

// Reverse transform is the directly provided function
const reversed = spec.transformRemoteToLocal!({server_name: 'My App'})
expect(reversed).toEqual({name: 'My App'})

// deployConfig is set
expect(spec.deployConfig).toBeDefined()
})

test('direct params take precedence over transformConfig-derived values', () => {
const directForward = (obj: object) => ({overridden: true})
const directReverse = (obj: object) => ({overridden: true})

const spec = createConfigExtensionSpecification({
identifier: 'test-module',
schema: TestSchema,
transformConfig: {server_name: 'name'},
transformLocalToRemote: directForward,
transformRemoteToLocal: directReverse,
})

// Direct params win over transformConfig-derived values
expect(spec.transformLocalToRemote!({name: 'test'}, {} as any)).toEqual({overridden: true})
expect(spec.transformRemoteToLocal!({server_name: 'test'})).toEqual({overridden: true})
})
})

describe('configWithoutFirstClassFields', () => {
test('removes the first class fields from the config', () => {
// When
Expand Down
16 changes: 13 additions & 3 deletions packages/app/src/cli/models/extensions/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,30 @@ export function createConfigExtensionSpecification<TConfiguration extends BaseCo
identifier: string
schema: ZodSchemaType<TConfiguration>
appModuleFeatures?: (config?: TConfiguration) => ExtensionFeature[]
transformConfig: TransformationConfig | CustomTransformationConfig
transformConfig?: TransformationConfig | CustomTransformationConfig
deployConfig?: ExtensionSpecification<TConfiguration>['deployConfig']
transformLocalToRemote?: ExtensionSpecification<TConfiguration>['transformLocalToRemote']
transformRemoteToLocal?: ExtensionSpecification<TConfiguration>['transformRemoteToLocal']
uidStrategy?: UidStrategy
getDevSessionUpdateMessages?: (config: TConfiguration) => Promise<string[]>
patchWithAppDevURLs?: (config: TConfiguration, urls: ApplicationURLs) => void
}): ExtensionSpecification<TConfiguration> {
const appModuleFeatures = spec.appModuleFeatures ?? (() => [])

const transformLocalToRemote =
spec.transformLocalToRemote ?? (spec.transformConfig ? resolveAppConfigTransform(spec.transformConfig) : undefined)
const transformRemoteToLocal =
spec.transformRemoteToLocal ?? resolveReverseAppConfigTransform(spec.schema, spec.transformConfig)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformRemoteToLocal is now always set even when transformConfig is omitted

createConfigExtensionSpecification now computes const transformRemoteToLocal = spec.transformRemoteToLocal ?? resolveReverseAppConfigTransform(spec.schema, spec.transformConfig) and resolveReverseAppConfigTransform returns a default reverse-transform when transformConfig is undefined. This means every config extension spec will have a reverse transform unless it explicitly sets transformRemoteToLocal: undefined (not really expressible) or bypasses the factory. Previously, transformConfig was required, so the behavior was explicit.

Why this is risky: callers relying on “no reverse transform” semantics may see keys dropped, content nested according to schema, or input mutation (see next comment), leading to confusing diffs or broken local config; and potentially omitting server fields if the transformed output is re-sent.

Scale: affects any configuration extension spec created without transformConfig (now allowed by types).


return createExtensionSpecification({
identifier: spec.identifier,
// This casting is required because `name` and `type` are mandatory for the existing extension spec configurations,
// however, app config extensions config content is parsed from the `shopify.app.toml`
schema: spec.schema,
appModuleFeatures,
transformLocalToRemote: resolveAppConfigTransform(spec.transformConfig),
transformRemoteToLocal: resolveReverseAppConfigTransform(spec.schema, spec.transformConfig),
deployConfig: spec.deployConfig,
transformLocalToRemote,
transformRemoteToLocal,
experience: 'configuration',
uidStrategy: spec.uidStrategy ?? 'single',
getDevSessionUpdateMessages: spec.getDevSessionUpdateMessages,
Expand Down
Loading