Skip to content

[hsjs] Better handling of JSON property names #7276

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-server-js"
---

Fixed an issue in which differences between model and JSON serialized property names were not correctly detected and property names for JSON serialization were not correctly quoted as necessary.
8 changes: 5 additions & 3 deletions packages/http-server-js/src/common/serialization/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { getHeaderFieldOptions, getPathParamOptions, getQueryParamOptions } from "@typespec/http";
import { JsContext, Module } from "../../ctx.js";
import { reportDiagnostic } from "../../lib.js";
import { access, parseCase } from "../../util/case.js";
import { access, objectLiteralProperty, parseCase } from "../../util/case.js";
import { differentiateUnion, writeCodeTree } from "../../util/differentiate.js";
import { UnimplementedError } from "../../util/error.js";
import { indent } from "../../util/iter.js";
Expand Down Expand Up @@ -106,10 +106,12 @@ function propertyRequiresJsonSerialization(
module: Module,
property: ModelProperty,
): boolean {
const encodedName = resolveEncodedName(ctx.program, property, "application/json");
const jsPropertyName = keywordSafe(parseCase(property.name).camelCase);
return !!(
isHttpMetadata(ctx, property) ||
getEncode(ctx.program, property) ||
resolveEncodedName(ctx.program, property, "application/json") !== property.name ||
encodedName !== jsPropertyName ||
(isJsonSerializable(property.type) &&
requiresJsonSerialization(ctx, module, property.type, property))
);
Expand Down Expand Up @@ -190,7 +192,7 @@ function* emitToJson(
expr = transposeExpressionToJson(ctx, property.type, expr, module);
}

yield ` ${encodedName}: ${expr},`;
yield ` ${objectLiteralProperty(encodedName)}: ${expr},`;
}

yield `};`;
Expand Down
13 changes: 13 additions & 0 deletions packages/http-server-js/src/util/case.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license.

import { KEYWORDS } from "./keywords.js";

/**
* Separators recognized by the case parser.
*/
Expand Down Expand Up @@ -30,6 +32,17 @@ export function isUnspeakable(name: string): boolean {

const JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;

/**
* Returns the property name to be used in an object literal.
*/
export function objectLiteralProperty(name: string): string {
if (!JS_IDENTIFIER.test(name) || KEYWORDS.has(name)) {
return JSON.stringify(name);
} else {
return name;
}
}

/**
* Returns an access expression for a given subject and key.
*
Expand Down
Loading