🐛(y-provider) return empty output when converting empty Yjs document

The convert handler returned 500 "No valid blocks were generated" whenever
the reader produced an empty blocks array, which is the normal state of a
freshly-created Yjs document. Treat empty input as a valid case and return
200 with an empty body for every supported output format.

Signed-off-by: Sylvain Zimmer <sylvain@sylvainzimmer.com>
This commit is contained in:
Sylvain Zimmer
2026-05-29 17:23:21 +02:00
committed by Anthony LC
parent 4841d9584f
commit 5bd2d1b9f1
3 changed files with 29 additions and 6 deletions
+1
View File
@@ -23,6 +23,7 @@ and this project adheres to
- 🐛(docs) run migration 0027 without superuser role
- 🐛(backend) prevent admins/owners from overwriting other users comments
- 🐛(y-provider) return empty output when converting empty Yjs document
- 🐛(backend) use computed_link_reach in handle_onboarding_document #2305
### Changed
@@ -315,4 +315,31 @@ describe('Conversion Testing', () => {
expect(response.body).toStrictEqual({ error: 'Invalid content' });
expect(destroySpy).toHaveBeenCalledTimes(1);
});
test('POST /api/convert empty Yjs document returns 200 with empty content', async () => {
const app = initApp();
const yjsUpdate = Y.encodeStateAsUpdate(new Y.Doc());
const htmlResponse = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', `Bearer ${apiKey}`)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'text/html')
.send(Buffer.from(yjsUpdate));
expect(htmlResponse.status).toBe(200);
expect(htmlResponse.text).toBe('');
const markdownResponse = await request(app)
.post('/api/convert')
.set('origin', origin)
.set('authorization', `Bearer ${apiKey}`)
.set('content-type', 'application/vnd.yjs.doc')
.set('accept', 'text/markdown')
.send(Buffer.from(yjsUpdate));
expect(markdownResponse.status).toBe(200);
expect(markdownResponse.text).toBe('\n');
});
});
@@ -151,15 +151,10 @@ export const convertHandler = async (
return;
}
if (!blocks || blocks.length === 0) {
res.status(500).json({ error: 'No valid blocks were generated' });
return;
}
res
.status(200)
.setHeader('content-type', accept)
.send(await writer.write(blocks));
.send(await writer.write(blocks ?? []));
} catch (e) {
logger('conversion failed:', e);
res.status(500).json({ error: 'An error occurred' });