Skip to content

Commit

Permalink
fix(editor): retrieve default value from dereferenced spec
Browse files Browse the repository at this point in the history
closes #212
  • Loading branch information
ostridm committed Sep 20, 2023
1 parent 749b6d8 commit 8acb8b2
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"@har-sdk/core": "*",
"@har-sdk/validator": "*",
"@har-sdk/types": "*",
"@types/jsonpath": "^0.2.0",
"js-yaml": "^4.1.0",
"json-pointer": "^0.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 33 additions & 8 deletions packages/editor/src/editor/oas/v2/OasV2ParameterObjectsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"children": [
{
"children": [
{
"jsonPointer": "/paths/~1pet/post",
"method": "POST",
"parameters": [
{
"bodyType": "application/json",
"paramType": "requestBody",
"value": {
"name": "Jack"
},
"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": "Jack"
},
"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": "/"
}
52 changes: 52 additions & 0 deletions packages/editor/tests/fixtures/oas2-default-param-value.yaml
Original file line number Diff line number Diff line change
@@ -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: Jack
19 changes: 19 additions & 0 deletions packages/editor/tests/oas2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8acb8b2

Please sign in to comment.