fix(httpapi): accept empty session create body (#24640)
This commit is contained in:
@@ -203,7 +203,7 @@ export const SessionApi = HttpApi.make("session")
|
|||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
HttpApiEndpoint.post("create", SessionPaths.create, {
|
HttpApiEndpoint.post("create", SessionPaths.create, {
|
||||||
payload: Session.CreateInput,
|
payload: [HttpApiSchema.NoContent, Session.CreateInput],
|
||||||
success: Session.Info,
|
success: Session.Info,
|
||||||
}).annotateMerge(
|
}).annotateMerge(
|
||||||
OpenApi.annotations({
|
OpenApi.annotations({
|
||||||
@@ -513,7 +513,7 @@ export const sessionHandlers = Layer.unwrap(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const create = Effect.fn("SessionHttpApi.create")(function* (ctx: { payload: Session.CreateInput }) {
|
const create = Effect.fn("SessionHttpApi.create")(function* (ctx: { payload?: Session.CreateInput }) {
|
||||||
const instance = yield* InstanceState.context
|
const instance = yield* InstanceState.context
|
||||||
return yield* Effect.promise(() =>
|
return yield* Effect.promise(() =>
|
||||||
Instance.restore(instance, () =>
|
Instance.restore(instance, () =>
|
||||||
@@ -524,6 +524,22 @@ export const sessionHandlers = Layer.unwrap(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const createRaw = Effect.fn("SessionHttpApi.createRaw")(function* (ctx: {
|
||||||
|
request: HttpServerRequest.HttpServerRequest
|
||||||
|
}) {
|
||||||
|
const body = yield* Effect.orDie(ctx.request.text)
|
||||||
|
if (body.trim().length === 0) return yield* create({})
|
||||||
|
|
||||||
|
const json = yield* Effect.try({
|
||||||
|
try: () => JSON.parse(body) as unknown,
|
||||||
|
catch: () => new HttpApiError.BadRequest({}),
|
||||||
|
})
|
||||||
|
const payload = yield* Schema.decodeUnknownEffect(Session.CreateInput)(json).pipe(
|
||||||
|
Effect.mapError(() => new HttpApiError.BadRequest({})),
|
||||||
|
)
|
||||||
|
return yield* create({ payload })
|
||||||
|
})
|
||||||
|
|
||||||
const remove = Effect.fn("SessionHttpApi.remove")(function* (ctx: { params: { sessionID: SessionID } }) {
|
const remove = Effect.fn("SessionHttpApi.remove")(function* (ctx: { params: { sessionID: SessionID } }) {
|
||||||
const instance = yield* InstanceState.context
|
const instance = yield* InstanceState.context
|
||||||
yield* Effect.promise(() =>
|
yield* Effect.promise(() =>
|
||||||
@@ -894,7 +910,7 @@ export const sessionHandlers = Layer.unwrap(
|
|||||||
.handle("diff", diff)
|
.handle("diff", diff)
|
||||||
.handle("messages", messages)
|
.handle("messages", messages)
|
||||||
.handle("message", message)
|
.handle("message", message)
|
||||||
.handle("create", create)
|
.handleRaw("create", createRaw)
|
||||||
.handle("remove", remove)
|
.handle("remove", remove)
|
||||||
.handle("update", update)
|
.handle("update", update)
|
||||||
.handle("fork", fork)
|
.handle("fork", fork)
|
||||||
|
|||||||
@@ -151,6 +151,14 @@ describe("session HttpApi", () => {
|
|||||||
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false, share: "disabled" } })
|
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false, share: "disabled" } })
|
||||||
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
||||||
|
|
||||||
|
const createdEmpty = await json<Session.Info>(
|
||||||
|
await app().request(SessionPaths.create, {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
expect(createdEmpty.id).toBeTruthy()
|
||||||
|
|
||||||
const created = await json<Session.Info>(
|
const created = await json<Session.Info>(
|
||||||
await app().request(SessionPaths.create, {
|
await app().request(SessionPaths.create, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|||||||
Reference in New Issue
Block a user