Skip to content

Commit

Permalink
fix(editor): retrieve parameter default value from dereferenced spec (#…
Browse files Browse the repository at this point in the history
…213)

closes #212
  • Loading branch information
ostridm committed Sep 20, 2023
1 parent 749b6d8 commit 18888f0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ 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'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"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"
}
],
"jsonPointer": "/",
"name": "Swagger Petstore",
"parameters": [
{
"name": "host",
"paramType": "variable",
"value": "petstore.swagger.io",
"valueJsonPointer": "/host"
}
],
"path": "/"
}
39 changes: 39 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,39 @@
swagger: '2.0'
info:
title: Swagger Petstore
version: 1.0.0
host: petstore.swagger.io
basePath: /v2
schemes:
- https
paths:
/pet:
post:
consumes:
- application/json
parameters:
- $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 18888f0

Please sign in to comment.