Skip to content

Commit

Permalink
fix(schema): Fix setting dispatch on existing nested objects (#3380)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jan 5, 2024
1 parent 550b09a commit 04efd5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/schema/src/hooks/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ export const resolveExternal =
const status = context.params.resolve
const { isPaginated, data } = getResult(context)
const resolveAndGetDispatch = async (current: any) => {
const currentExistingDispatch = getDispatch(current)

if (currentExistingDispatch !== null) {
return currentExistingDispatch
}

const resolved = await runResolvers(resolvers, current, context, status)
const currentDispatch = Object.keys(resolved).reduce(
(res, key) => {
Expand Down
19 changes: 18 additions & 1 deletion packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
FromSchema,
getValidator,
getDataValidator,
virtual
virtual,
resolveExternal
} from '../src'

const fixtureAjv = new Ajv({
Expand Down Expand Up @@ -198,6 +199,13 @@ class MessageService extends MemoryService<Message, MessageData, ServiceParams>
}
}

const findResult = { message: 'Hello' }
class CustomService {
async find() {
return [findResult]
}
}

const customMethodDataResolver = resolve<any, HookContext<Application>>({
properties: {
userId: async () => 0,
Expand All @@ -209,6 +217,7 @@ type ServiceTypes = {
users: MemoryService<User, UserData, ServiceParams>
messages: MessageService
paginatedMessages: MemoryService<Message, MessageData, ServiceParams>
custom: CustomService
}
type Application = FeathersApplication<ServiceTypes>

Expand All @@ -223,6 +232,14 @@ app.use(
app.use('messages', new MessageService(), {
methods: ['find', 'get', 'create', 'update', 'patch', 'remove', 'customMethod']
})
app.use('custom', new CustomService())

app.service('custom').hooks({
around: {
all: [resolveExternal(resolve<any, HookContext<Application>>({}))]
}
})

app.use('paginatedMessages', memory({ paginate: { default: 10 } }))

app.service('messages').hooks({
Expand Down
7 changes: 7 additions & 0 deletions packages/schema/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ describe('@feathersjs/schema/hooks', () => {
})
})

it('resolves safe dispatch with static data', async () => {
const service = app.service('custom')

await service.find()
assert.deepStrictEqual(await service.find(), [{ message: 'Hello' }])
})

it('resolves data for custom methods', async () => {
const result = await app.service('messages').customMethod({ message: 'Hello' })
const user = {
Expand Down

0 comments on commit 04efd5a

Please sign in to comment.