Skip to content

Commit

Permalink
feat: make it possible to fetch schema from parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Gunnarsson authored and tgun89 committed Aug 16, 2019
1 parent 372fed9 commit 82ce53c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ParameterObject, ParameterStyle, SchemaObject, ReferenceObject, ExampleObject } from 'openapi3-ts';
import { ParameterObject, ParameterStyle, ReferenceObject, ExampleObject } from 'openapi3-ts';
import { Content, ContentSchemas } from './content';
import { Schema } from '@hapi/joi';
import { createSwaggerDefinition } from './utils/create_swagger_definition';

class BaggerContentDefinedForParameterError extends Error {}
class BaggerSchemaDefinedForParameterError extends Error {}

interface ExamplesObject {
[param: string]: ExampleObject | ReferenceObject;
Expand All @@ -11,6 +15,7 @@ export type ParameterType = 'path' | 'query' | 'cookie' | 'header';
export class BaggerParameter {
private settings: ParameterObject;
private _content: Content = new Content();
private _schema?: Schema;

public constructor(type: ParameterType, name: string) {
this.settings = { in: type, name };
Expand Down Expand Up @@ -89,8 +94,11 @@ export class BaggerParameter {
return this;
}

public schema(schema: SchemaObject | ReferenceObject): BaggerParameter {
this.settings.schema = schema;
public schema(schema: Schema): BaggerParameter {
if (Object.keys(this._content.getSchemas()).length > 0) {
throw new BaggerContentDefinedForParameterError();
}
this._schema = schema;
return this;
}

Expand All @@ -100,16 +108,32 @@ export class BaggerParameter {
}

public addContent(contentType: string, schema: Schema): BaggerParameter {
if (this.settings.schema) {
throw new BaggerSchemaDefinedForParameterError();
}
this._content.add(contentType, schema);
return this;
}

public getSchemas(): ContentSchemas {
if (this._schema) {
return {
'application/json': this._schema
};
}
return this._content.getSchemas();
}

public compile(): ParameterObject {
this.settings.content = this._content.compile();
if (this._schema) {
this.settings.schema = {
'application/json': {
schema: createSwaggerDefinition(this._schema)
}
};
} else {
this.settings.content = this._content.compile();
}
return this.settings;
}
}
19 changes: 19 additions & 0 deletions test/__snapshots__/parameters.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Parameters Add path parameter with schema 1`] = `
Object {
"in": "path",
"name": "bagType",
"schema": Object {
"application/json": Object {
"schema": Object {
"enum": Array [
"backpack",
"duffel",
],
"type": "string",
},
},
},
}
`;
16 changes: 16 additions & 0 deletions test/parameters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as joi from '@hapi/joi';
import { BaggerParameter } from '../lib/parameters';

describe('Parameters', () => {
test('Add path parameter with schema', () => {
const schema = joi.string().valid(['backpack', 'duffel']);
const parameter = new BaggerParameter('path', 'bagType');
parameter.schema(schema);
expect(parameter.compile()).toMatchSnapshot();
const actualSchema = parameter.getSchemas();
const expectedSchema = {
'application/json': schema
};
expect(actualSchema).toEqual(expectedSchema);
});
});

0 comments on commit 82ce53c

Please sign in to comment.