Skip to content
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

Allow for different resource content keys than application/json #73

Open
wants to merge 1 commit into
base: master
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
12 changes: 10 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export function transformToGenerateResultFunctions(
const useFaker = options?.ai?.enable !== true;

if (useFaker) {
const fakerResult = transformJSONSchemaToFakerCode(r.responses?.['application/json']);
if (!r.responses) {
return;
}
const jsonResponseKey = Object.keys(r.responses).filter(r => r.startsWith('application/json'))[0];
const fakerResult = transformJSONSchemaToFakerCode(r.responses?.[jsonResponseKey]);
if (options?.static) {
vm.runInContext(`result = ${fakerResult};`, context);
}
Expand All @@ -67,7 +71,11 @@ export function transformToGenerateResultFunctions(
].join('\n');
}

const operationString = JSON.stringify(r.responses?.['application/json'], null, 4);
if (!r.responses) {
return;
}
const jsonResponseKey = Object.keys(r.responses).filter(r => r.startsWith('application/json'))[0];
const operationString = JSON.stringify(r.responses?.[jsonResponseKey], null, 4);
return [
`export async function `,
`${name}() { `,
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ paths:
'200':
description: OK
content:
application/json:
application/json; ; charset=utf-8;:
schema:
allOf:
- $ref: '#/components/schemas/OkResponse'
Expand Down
2 changes: 1 addition & 1 deletion test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('generate:generateOperationCollection', () => {
let schema: OpenAPIV3.SchemaObject;
beforeAll(async () => {
const collection = await generateCollectionFromSpec('./test/fixture/test.yaml');
schema = get(collection, [0, 'response', '0', 'responses', 'application/json', 'allOf', 1]);
schema = get(collection, [0, 'response', '0', 'responses', 'application/json; ; charset=utf-8;', 'allOf', 1]);
});

it('schema should be defined', () => {
Expand Down