fix: Omit undefined optional fields from deserialised output#1488
Conversation
Greptile OverviewGreptile SummaryThis PR improves the SDK's type safety by ensuring that optional fields absent from API responses are omitted from deserialized objects rather than explicitly set to Key improvements:
The implementation is consistent, well-tested, and maintains backward compatibility for consumers who check for field presence using Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant API as WorkOS API
participant SDK as SDK Client
participant Serializer as Deserializer
participant App as Application
API->>SDK: API Response (snake_case)<br/>{organization_id: undefined}
SDK->>Serializer: deserializeProfile(response)
alt Before this PR
Serializer->>Serializer: organizationId: response.organization_id
Serializer->>App: {organizationId: undefined, ...}
Note over App: Field present but undefined
end
alt After this PR
Serializer->>Serializer: ...(response.organization_id !== undefined && {organizationId})
Serializer->>App: {id, email, ...}<br/>(organizationId omitted)
Note over App: Field not present in object
end
API->>SDK: Pagination Response<br/>{after: null, before: "cursor"}
SDK->>Serializer: deserializeListObjects(response)
alt After this PR
Serializer->>Serializer: Type: string | null
Serializer->>App: {listMetadata: {after: null, before: "cursor"}}
Note over App: null preserved (semantic meaning)
end
Last reviewed commit: 862bedc |
862bedc to
ca081aa
Compare
…cluding them as undefined
When an optional field is not present in an API response, the
deserialiser should not include the key at all rather than setting
it to `undefined`. This change switches every affected deserialiser
to the spread-guard pattern `...(value !== undefined && { key })`,
which preserves explicit `null` values while omitting truly absent
keys.
Also updates the `List` / `ListResponse` pagination cursor types
from `string` to `string | null` so that the `null` values the API
actually returns are represented faithfully.
6d33451 to
656a48f
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 656a48f0c5
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
HttpClient.getQueryString previously only stripped empty strings and undefined, allowing null to be serialised as the literal query value "null". This fixes getQueryString to also reject null, and updates PaginationOptions and ListWarrantsOptions to accept string | null so that API response cursors can be passed straight through.
656a48f to
1a510e7
Compare
|
looks good, thank you! |
Summary
...(value !== undefined && { key })spread-guard pattern so that optional fields absent from an API response are omitted entirely rather than included asundefinednullvalues (e.g.verificationToken: null) which are semantically meaningfulList/ListResponsepagination cursor types fromstringtostring | nullto match the values the API actually returnsPaginationOptionsandListWarrantsOptionsto acceptstring | nullforbefore/after, so that pagination cursors from API responses can be passed straight through without conversionAffected deserialisers
session.serializer.tsorganizationId,impersonatorconnection.serializer.tsorganizationIdauthentication-event.serializer.tserrorprofile.serializer.tsorganizationId,firstName,lastName,role,roles,groups,customAttributes,rawAttributesdirectory-user.serializer.tsrole,roles(bothdeserializeDirectoryUseranddeserializeUpdatedEventDirectoryUser)organization-domain.serializer.tsverificationTokenvault-object.serializer.tsvalue,listMetadata.after,listMetadata.beforePagination cursor type changes
List.listMetadatabefore,afterstringstring | nullListResponse.list_metadatabefore,afterstringstring | nullPaginationOptionsbefore,afterstringstring | nullListWarrantsOptionsafterstringstring | nullSerializedListWarrantsOptionsafterstringstring | nullDiscussion: Pagination cursor type (
stringvsstring | null)The WorkOS API documentation defines the
afterandbeforepagination cursors as typestring, yet the response examples clearly returnnullwhen there is no further page:{ "data": [...], "list_metadata": { "after": null, "before": "object_id_xxx" } }For example, see the Vault list objects reference.
This PR updates both the response types (
List,ListResponse) and the input types (PaginationOptions,ListWarrantsOptions) tostring | null. This means pagination cursors from API responses can be passed directly back into list options without any coercion (e.g.?? undefined), making the API ergonomic and type-honest.If the documentation is authoritative and the API should be returning
undefined(i.e. omitting the key) instead ofnull, then a server-side change may be needed. Either way, the SDK types should match the actual response shape, and the input types should accept whatever the output types produce.Test plan
npx tsc --noEmitpasses