Skip to content

Commit

Permalink
feat(parser): update example handling to support examples array
Browse files Browse the repository at this point in the history
  • Loading branch information
mosch committed Jan 15, 2025
1 parent 8a0bb27 commit 57c3b6e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
7 changes: 6 additions & 1 deletion examples/with-openapi-json/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@
"description": "Update an existent pet in the store",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Pet" }
"schema": { "$ref": "#/components/schemas/Pet" },
"example": {
"id": "123",
"name": "Dog",
"status": "available"
}
},
"application/xml": {
"schema": { "$ref": "#/components/schemas/Pet" }
Expand Down
2 changes: 1 addition & 1 deletion packages/zudoku/src/lib/oas/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ const OperationItem = builder
mediaType,
schema: content.schema,
examples: Object.entries(content.examples ?? {}).map(
([name, value]) => ({ name, ...value }),
([name, value]) => ({ name, value }),
),
encoding: Object.entries(content.encoding ?? {}).map(
([name, value]) => ({ name, ...value }),
Expand Down
6 changes: 2 additions & 4 deletions packages/zudoku/src/lib/oas/parser/upgrade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ export const upgradeSchema = (schema: RecordAny): OpenAPIDocument => {
});

schema = traverse(schema, (sub) => {
if (sub.example !== undefined) {
sub.examples = {
default: sub.example,
};
if (sub.example !== undefined && sub.examples === undefined) {
sub.examples = [sub.example];
delete sub.example;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export const Sidecar = ({

const code = useMemo(() => {
const example = requestBodyContent?.[0]?.schema
? generateSchemaExample(requestBodyContent[0].schema as SchemaObject)
? (requestBodyContent.at(0)?.examples?.at(0)?.value ??
generateSchemaExample(requestBodyContent[0].schema as SchemaObject))
: undefined;

const snippet = new HTTPSnippet({
Expand Down

0 comments on commit 57c3b6e

Please sign in to comment.