Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): Fix setting dispatch on existing nested objects #3380

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
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
Loading