From 40167183838e0420e3e57fc717684c698e4e36d2 Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Wed, 20 Sep 2023 10:16:54 +0300 Subject: [PATCH] fix(editor): retrieve default value from dereferenced spec closes #212 --- .../oas/BaseOasParameterObjectsParser.ts | 2 +- .../oas/v2/OasV2ParameterObjectsParser.ts | 41 +++++++++--- .../oas2-default-param-value.result.json | 63 +++++++++++++++++++ .../fixtures/oas2-default-param-value.yaml | 52 +++++++++++++++ packages/editor/tests/oas2.spec.ts | 19 ++++++ 5 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 packages/editor/tests/fixtures/oas2-default-param-value.result.json create mode 100644 packages/editor/tests/fixtures/oas2-default-param-value.yaml diff --git a/packages/editor/src/editor/oas/BaseOasParameterObjectsParser.ts b/packages/editor/src/editor/oas/BaseOasParameterObjectsParser.ts index 682196f1..2edcc4a1 100644 --- a/packages/editor/src/editor/oas/BaseOasParameterObjectsParser.ts +++ b/packages/editor/src/editor/oas/BaseOasParameterObjectsParser.ts @@ -18,7 +18,7 @@ export abstract class BaseOasParameterObjectsParser< { protected constructor( protected readonly doc: D, - private readonly dereferencedDoc: D + protected readonly dereferencedDoc: D ) {} protected abstract getParameterValue( diff --git a/packages/editor/src/editor/oas/v2/OasV2ParameterObjectsParser.ts b/packages/editor/src/editor/oas/v2/OasV2ParameterObjectsParser.ts index 54e12408..5ecf39c4 100644 --- a/packages/editor/src/editor/oas/v2/OasV2ParameterObjectsParser.ts +++ b/packages/editor/src/editor/oas/v2/OasV2ParameterObjectsParser.ts @@ -6,11 +6,16 @@ import { import { BaseOasParameterObjectsParser } from '../BaseOasParameterObjectsParser'; import jsonPointer from 'json-pointer'; import { OpenAPIV2 } from '@har-sdk/core'; +import { isOASV2 } from '@har-sdk/types'; export class OasV2ParameterObjectsParser extends BaseOasParameterObjectsParser< OpenAPIV2.Document, OpenAPIV2.ReferenceObject | OpenAPIV2.Parameter > { + private readonly DEFAULT_CONSUME_MEDIA_TYPE: OpenAPIV2.MimeTypes = [ + 'application/json' + ]; + constructor(doc: OpenAPIV2.Document, dereferencedDoc: OpenAPIV2.Document) { super(doc, dereferencedDoc); } @@ -70,17 +75,37 @@ export class OasV2ParameterObjectsParser extends BaseOasParameterObjectsParser< parameter: OpenAPIV2.Parameter ): SpecTreeRequestBodyParam[] { const operationObject: OpenAPIV2.OperationObject = jsonPointer.get( - this.doc, + this.dereferencedDoc, jsonPointer.compile(jsonPointer.parse(pointer).slice(0, -2)) ); - const mimeTypes = operationObject.consumes || ['application/json']; + const value = parameter.schema?.default; - return mimeTypes.map((mimeType) => ({ - paramType: 'requestBody', - bodyType: mimeType, - ...(value != null ? { value } : {}), - valueJsonPointer: this.getValueJsonPointer(`${pointer}/schema`) - })); + return this.resolveOperationConsumeMediaTypes(operationObject).map( + (mediaType) => ({ + paramType: 'requestBody', + bodyType: mediaType, + ...(value != null ? { value } : {}), + valueJsonPointer: this.getValueJsonPointer(`${pointer}/schema`) + }) + ); + } + + private resolveOperationConsumeMediaTypes( + operation: OpenAPIV2.OperationObject + ): OpenAPIV2.MimeTypes { + let mediaTypes: OpenAPIV2.MimeTypes; + + if (operation.consumes?.length) { + mediaTypes = operation.consumes; + } else if (isOASV2(this.doc) && this.doc.consumes?.length) { + mediaTypes = this.doc.consumes; + } + + mediaTypes = mediaTypes + ?.map((mediaType) => mediaType?.trim()) + .filter(Boolean); + + return mediaTypes?.length ? mediaTypes : this.DEFAULT_CONSUME_MEDIA_TYPE; } } diff --git a/packages/editor/tests/fixtures/oas2-default-param-value.result.json b/packages/editor/tests/fixtures/oas2-default-param-value.result.json new file mode 100644 index 00000000..926eb6f6 --- /dev/null +++ b/packages/editor/tests/fixtures/oas2-default-param-value.result.json @@ -0,0 +1,63 @@ +{ + "children": [ + { + "children": [ + { + "jsonPointer": "/paths/~1pet/post", + "method": "POST", + "parameters": [ + { + "bodyType": "application/json", + "paramType": "requestBody", + "value": { + "name": "doggie" + }, + "valueJsonPointer": "/paths/~1pet/post/parameters/0/schema/default" + } + ], + "path": "/pet" + } + ], + "jsonPointer": "/paths/~1pet", + "path": "/pet" + }, + { + "children": [ + { + "jsonPointer": "/paths/~1pet~1{petId}/put", + "method": "PUT", + "parameters": [ + { + "location": "path", + "name": "petId", + "paramType": "location", + "valueJsonPointer": "/paths/~1pet~1{petId}/put/parameters/0/default" + }, + { + "bodyType": "application/xml", + "paramType": "requestBody", + "value": { + "name": "doggie" + }, + "valueJsonPointer": "/paths/~1pet~1{petId}/put/parameters/1/schema/default" + } + ], + "path": "/pet/{petId}" + } + ], + "jsonPointer": "/paths/~1pet~1{petId}", + "path": "/pet/{petId}" + } + ], + "jsonPointer": "/", + "name": "Swagger Petstore", + "parameters": [ + { + "name": "host", + "paramType": "variable", + "value": "petstore.swagger.io", + "valueJsonPointer": "/host" + } + ], + "path": "/" +} diff --git a/packages/editor/tests/fixtures/oas2-default-param-value.yaml b/packages/editor/tests/fixtures/oas2-default-param-value.yaml new file mode 100644 index 00000000..094b7c5b --- /dev/null +++ b/packages/editor/tests/fixtures/oas2-default-param-value.yaml @@ -0,0 +1,52 @@ +swagger: '2.0' +info: + title: Swagger Petstore + version: 1.0.0 +host: petstore.swagger.io +basePath: /v2 +schemes: + - https +paths: + /pet: + post: + parameters: + - $ref: '#/parameters/ReferencedPostBodyParam' + responses: + 200: + description: successful operation + /pet/{petId}: + put: + consumes: + - application/xml + parameters: + - description: Pet id to patch + format: int64 + in: path + name: petId + required: true + type: integer + - $ref: '#/parameters/ReferencedPostBodyParam' + responses: + 200: + description: successful operation +parameters: + ReferencedPostBodyParam: + description: A JSON object containing pet information + in: body + name: body + required: true + schema: + $ref: '#/definitions/Pet' +definitions: + Pet: + properties: + name: + example: doggie + type: string + required: + - name + type: object + xml: + name: Pet + default: + name: doggie diff --git a/packages/editor/tests/oas2.spec.ts b/packages/editor/tests/oas2.spec.ts index d91c6a52..73f267ea 100644 --- a/packages/editor/tests/oas2.spec.ts +++ b/packages/editor/tests/oas2.spec.ts @@ -73,6 +73,25 @@ describe('OasV2Editor', () => { expect(result).toEqual(expected); }); + it('should retrieve default parameter value', async () => { + const sourceYaml = readFileSync( + resolve(__dirname, './fixtures/oas2-default-param-value.yaml'), + 'utf-8' + ); + await openApiParser.setup(sourceYaml); + + const expected = JSON.parse( + readFileSync( + resolve(__dirname, './fixtures/oas2-default-param-value.result.json'), + 'utf-8' + ) + ); + + const result = openApiParser.parse(); + + expect(result).toEqual(expected); + }); + it('should be exception on call "parse" before "setup"', () => expect(() => openApiParser.parse()).toThrowError( 'You have to call "setup" to initialize the document'