typespec icon indicating copy to clipboard operation
typespec copied to clipboard

[Bug]: http-server-js: kebab case model properties not emitted as valid JS code in JSON serializer

Open tjb1982 opened this issue 10 months ago • 2 comments

Describe the bug

When using kebab case properties, like

model CommonHeaders {
    @header `content-length`: uint64;
}

emitToJson fails to produce the toJsonObject method with valid JavaScript -- prettier fails here:

https://github.com/microsoft/typespec/blob/main/packages/http-server-js/src/write.ts#L82

This is because emitToJson emits properties as is here

https://github.com/microsoft/typespec/blob/main/packages/http-server-js/src/common/serialization/json.ts#L188

May suggest wrapping the properties in "

Reproduction

import "@typespec/http";

using TypeSpec.Http;

model Widget {
    id: int32;
    `parent-id`: int32; // <--
    name: string;
}

model WidgetResponse {
    ...Http.OkResponse;
    ...Http.Body<Widget>;
}

@service(#{ title: "Widgets", version: "1" })
@server("https://widgets.com", "foo")
@route("/widgets/")
namespace Foo {
    @get
    op getWidget(@path id: int32): WidgetResponse;
} 

Checklist

tjb1982 avatar Apr 07 '25 12:04 tjb1982

Actually, there is probably a second bug here -- entries marked as @header shouldn't be included in the serialization here, right?

tjb1982 avatar Apr 07 '25 12:04 tjb1982

@tjb1982 I'm going to put up a PR that addresses the naming issue alone and close this issue. The problem with metadata like headers nested within the body depends on an extensive rework that's tracked in https://github.com/microsoft/typespec/issues/6934. Basically the current implementation treats all data sources in the request/response separately (headers, query, body, etc.) and when they're mixed together the serializer just fails at handling all that correctly, so I think I need to refactor the handling logic there to parse/serialize everything in a single pass rather than try to do it piecemeal like I'm doing now.

witemple-msft avatar May 07 '25 20:05 witemple-msft