-
Notifications
You must be signed in to change notification settings - Fork 12
feat(agent-bff): publish typed record schemas in the openapi document #1867
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
Tonours
wants to merge
6
commits into
main
Choose a base branch
from
feat/prd-1105-typed-list-records
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
00ae5a4
feat(agent-bff): publish typed record schemas in the openapi document
Tonours 6a1d896
fix(agent-bff): keep the base field description under a camelized rec…
Tonours 1c5eee8
fix(agent-bff): publish record fields as nullable and share the harness
Tonours ed25484
style(agent-bff): reattach the collapse note to its function
Tonours 5c653c4
fix(agent-bff): widen nested record schema items and properties to nu…
Tonours 848ec87
test(agent-bff): unexport unused live-agent-harness constants
Tonours File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import type { ProjectableField, UnfoldedCollection } from './unfolding'; | ||
| import type { ReferenceObject, SchemaObject } from 'openapi3-ts/oas31'; | ||
|
|
||
| import Inflector from 'inflected'; | ||
|
|
||
| import toFieldSchema from './field-schemas'; | ||
| import { quoted } from './names'; | ||
| import { PACKED_ID_SEPARATOR } from '../data/pack-id'; | ||
|
|
||
| /** | ||
| * The key a field really carries in a response record. `agent-client` deserializes the agent's | ||
| * JSON:API with `keyForAttribute: 'camelCase'` (`http-requester.ts`), which is exactly this pair of | ||
| * `inflected` calls (`jsonapi-serializer/lib/inflector.js`), so a `first_name` column is PROJECTED | ||
| * under that name and RETURNED as `firstName`. The same library rather than a transcription: the | ||
| * transform handles acronyms and non-ASCII, and a mirror would drift from the deserializer without | ||
| * anything failing. | ||
| */ | ||
| export function recordKey(field: string): string { | ||
| return Inflector.camelize(Inflector.underscore(field), false); | ||
| } | ||
|
|
||
| // The flat id is the JSON:API resource id, which is a string by specification whatever the key | ||
| // column holds. `__forest.primaryKey` is the same id unpacked and typed, so the two forms of one | ||
| // record disagree by construction — the trap this schema exists to name. | ||
| const ID_SCHEMA: SchemaObject = { | ||
| type: 'string', | ||
| description: | ||
| 'The record id, always a string — the agent serializes it as the JSON:API resource id, even ' + | ||
| 'when the key column is a Number. `__forest.primaryKey` carries the same id TYPED, so ' + | ||
| `comparing the two without coercion fails. A composite key is its values joined by ` + | ||
| `${quoted(PACKED_ID_SEPARATOR)}.`, | ||
| }; | ||
|
|
||
| function isReference(schema: SchemaObject | ReferenceObject): schema is ReferenceObject { | ||
| return '$ref' in schema; | ||
| } | ||
|
|
||
| /** | ||
| * Every published field is nullable. The capabilities report a column type and never its | ||
| * nullability, so a nullable column answers `null` against a type this schema would otherwise | ||
| * declare non-null — a generated client validating the response would reject what the runtime | ||
| * really sends. An unconstrained schema already accepts null and is left alone. | ||
| */ | ||
| function nullable(schema: SchemaObject): SchemaObject { | ||
| if (typeof schema.type !== 'string') return schema; | ||
|
|
||
| const widened: SchemaObject = { ...schema, type: [schema.type, 'null'] }; | ||
|
|
||
| if (schema.items !== undefined && !isReference(schema.items)) { | ||
| widened.items = nullable(schema.items); | ||
| } | ||
|
|
||
| if (schema.properties !== undefined) { | ||
| widened.properties = Object.fromEntries( | ||
| Object.entries(schema.properties).map(([key, nested]) => [ | ||
| key, | ||
| isReference(nested) ? nested : nullable(nested), | ||
| ]), | ||
| ); | ||
| } | ||
|
|
||
| return widened; | ||
| } | ||
|
|
||
| /** | ||
| * The schema of one field, under the key the response carries it as. Several fields can collapse to | ||
| * the same key — the transform is lossy, `first_name` and `firstName` both yield `firstName` — and | ||
| * which one wins depends on the order the agent serialized them in, so the type is left open rather | ||
| * than picked. | ||
| */ | ||
| function propertySchema(key: string, fields: ProjectableField[]): SchemaObject { | ||
| const names = fields.map(field => quoted(field.name)).join(', '); | ||
|
|
||
| if (fields.length > 1) { | ||
| return { | ||
| description: | ||
| `${names} all reach the response under this single key, so which one it holds is not ` + | ||
| 'determined here. Project one of them at a time to know.', | ||
| }; | ||
| } | ||
|
Tonours marked this conversation as resolved.
|
||
|
|
||
| const [field] = fields; | ||
| const schema = nullable(toFieldSchema(field.type)); | ||
|
|
||
| if (field.name === key) return schema; | ||
|
|
||
| const description = [schema.description, `The ${names} field.`].filter(Boolean).join(' '); | ||
|
|
||
| return { ...schema, description }; | ||
| } | ||
|
|
||
| function fieldProperties(projectable: ProjectableField[]): Record<string, SchemaObject> { | ||
| const byKey = new Map<string, ProjectableField[]>(); | ||
|
|
||
| projectable.forEach(field => { | ||
| const key = recordKey(field.name); | ||
| const collapsed = byKey.get(key); | ||
|
|
||
| if (collapsed) collapsed.push(field); | ||
| else byKey.set(key, [field]); | ||
| }); | ||
|
|
||
| return Object.fromEntries([...byKey].map(([key, fields]) => [key, propertySchema(key, fields)])); | ||
| } | ||
|
|
||
| /** | ||
| * The record shape of one collection. The properties are what the capabilities report, and nothing | ||
| * more is forbidden: the capabilities route leaves out a OneToOne relation that the default | ||
| * projection returns, so a closed schema would reject responses the runtime really sends. `id` wins | ||
| * over any field of that name, because the deserializer overwrites the attribute with the resource | ||
| * id. | ||
| */ | ||
| export default function recordSchema( | ||
| collection: UnfoldedCollection, | ||
| forestMeta: ReferenceObject, | ||
| ): SchemaObject { | ||
| return { | ||
| type: 'object', | ||
| description: | ||
| `A record of ${quoted(collection.name)}. It carries the fields the request projected — ` + | ||
| 'omit `projection` and the agent returns them all. The properties below are the ones the ' + | ||
| "collection's capabilities report; a record can carry more, such as a to-one relation. " + | ||
| 'Every field is nullable: the capabilities report a column type and never its nullability, ' + | ||
| 'so a null is always possible whatever the type says.', | ||
| properties: { | ||
| ...fieldProperties(collection.fields.projectable), | ||
| id: ID_SCHEMA, | ||
| __forest: forestMeta, | ||
| }, | ||
| required: ['id', '__forest'], | ||
| }; | ||
| } | ||
|
|
||
| export function listResponseSchema( | ||
| collection: UnfoldedCollection, | ||
| record: ReferenceObject, | ||
| ): SchemaObject { | ||
| return { | ||
| type: 'object', | ||
| description: | ||
| `A page of ${quoted(collection.name)} records. The list never carries a total: call the ` + | ||
| 'count endpoint for that, which is why `countStatus` is always `not_requested`.', | ||
| properties: { | ||
| data: { type: 'array', items: record }, | ||
| meta: { | ||
| type: 'object', | ||
| properties: { countStatus: { type: 'string', const: 'not_requested' } }, | ||
| required: ['countStatus'], | ||
| }, | ||
| }, | ||
| required: ['data', 'meta'], | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nullable()stops at the container, soitemsand a nested object'spropertiesstay non-null — the same failure mode one level down, while the record prose says a null is always possible whatever the type says. Recurse, or narrow the prose to the top level?