Skip to content

Commit

Permalink
(fix, typescript): serialize optional deep object query params correc…
Browse files Browse the repository at this point in the history
…tly in the TypeScript SDK (#3071)

* (fix): ts sdk properly serializes optional objects

* add changelog and version bump for ts sdk

* Update CHANGELOG.md

* fix test
  • Loading branch information
dsinghvi authored Feb 27, 2024
1 parent 7a16b67 commit 4f69eee
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 12 deletions.
26 changes: 26 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.12.1] - 2024-02-27

- Fix: Optional objects in deep query parameters were previously being incorrectly
serialized. Before this change, optional objects were just being JSON.stringified
which would send the incorrect contents over the wire.

```ts
// Before
if (foo != null) {
_queryParams["foo"] = JSON.stringify(foo);
}

// After
if (foo != null) {
_queryParams["foo"] = foo;
}

// After (with serde layer)
if (foo != null) {
_queryParams["foo"] = serializers.Foo.jsonOrThrow(foo, {
skipValidation: false,
breadcrumbs: ["request", "foo"]
})
}
```

## [0.12.0] - 2024-02-26

- Feature: support deep object query parameter serialization. If, query parameters are
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0
0.12.1
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,23 @@ export class GeneratedQueryParams {

private getObjectType(typeReference: TypeReference, context: SdkContext): DeclaredTypeName | undefined {
switch (typeReference.type) {
case "named": {
const typeDeclaration = context.type.getTypeDeclaration(typeReference);
switch (typeDeclaration.shape.type) {
case "object":
return typeReference;
case "alias": {
return this.getObjectType(typeDeclaration.shape.aliasOf, context);
case "named":
{
const typeDeclaration = context.type.getTypeDeclaration(typeReference);
switch (typeDeclaration.shape.type) {
case "object":
return typeReference;
case "alias": {
return this.getObjectType(typeDeclaration.shape.aliasOf, context);
}
}
}
break;
case "container": {
switch (typeReference.container.type) {
case "optional":
return this.getObjectType(typeReference.container.optional, context);
}
}
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,105 @@ exports[`generate IR 1`] = `
"typeId": "type_user:NestedUser",
},
},
{
"allowMultiple": false,
"availability": null,
"docs": null,
"name": {
"name": {
"camelCase": {
"safeName": "optionalUser",
"unsafeName": "optionalUser",
},
"originalName": "optionalUser",
"pascalCase": {
"safeName": "OptionalUser",
"unsafeName": "OptionalUser",
},
"screamingSnakeCase": {
"safeName": "OPTIONAL_USER",
"unsafeName": "OPTIONAL_USER",
},
"snakeCase": {
"safeName": "optional_user",
"unsafeName": "optional_user",
},
},
"wireValue": "optionalUser",
},
"valueType": {
"_type": "container",
"container": {
"_type": "optional",
"optional": {
"_type": "named",
"fernFilepath": {
"allParts": [
{
"camelCase": {
"safeName": "user",
"unsafeName": "user",
},
"originalName": "user",
"pascalCase": {
"safeName": "User",
"unsafeName": "User",
},
"screamingSnakeCase": {
"safeName": "USER",
"unsafeName": "USER",
},
"snakeCase": {
"safeName": "user",
"unsafeName": "user",
},
},
],
"file": {
"camelCase": {
"safeName": "user",
"unsafeName": "user",
},
"originalName": "user",
"pascalCase": {
"safeName": "User",
"unsafeName": "User",
},
"screamingSnakeCase": {
"safeName": "USER",
"unsafeName": "USER",
},
"snakeCase": {
"safeName": "user",
"unsafeName": "user",
},
},
"packagePath": [],
},
"name": {
"camelCase": {
"safeName": "user",
"unsafeName": "user",
},
"originalName": "User",
"pascalCase": {
"safeName": "User",
"unsafeName": "User",
},
"screamingSnakeCase": {
"safeName": "USER",
"unsafeName": "USER",
},
"snakeCase": {
"safeName": "user",
"unsafeName": "user",
},
},
"typeId": "type_user:User",
},
},
},
},
{
"allowMultiple": true,
"availability": null,
Expand Down
97 changes: 97 additions & 0 deletions seed/csharp-model/query-parameters/ir.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions seed/csharp-sdk/query-parameters/ir.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-fiber/query-parameters/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-sdk/query-parameters/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f69eee

Please sign in to comment.