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

fix(editor): retrieve parameter default value from dereferenced spec #213

Merged
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
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