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

feat(repository-json-schema): add in top-level metadata for json schema #907

Merged
merged 2 commits into from
Jan 30, 2018
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 @@ -61,6 +61,7 @@ describe('Routing metadata for parameters', () => {
const actualSpec = getControllerSpec(MyController);
expect(actualSpec.definitions).to.deepEqual({
MyData: {
title: 'MyData',
properties: {
name: {
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,15 @@ describe('Routing metadata for parameters', () => {
// tslint:disable-next-line:no-any
expect(defs).to.have.keys('Foo', 'Bar');
expect(defs.Foo).to.deepEqual({
title: 'Foo',
properties: {
price: {
type: 'number',
},
},
});
expect(defs.Bar).to.deepEqual({
title: 'Bar',
properties: {
name: {
type: 'string',
Expand Down Expand Up @@ -354,7 +356,7 @@ describe('Routing metadata for parameters', () => {
expect(defs.MyBody).to.not.have.key('definitions');
});

it('infers empty body parameter schema if no property metadata is present', () => {
it('infers no properties if no property metadata is present', () => {
const paramSpec: ParameterObject = {
name: 'foo',
in: 'body',
Expand All @@ -372,7 +374,7 @@ describe('Routing metadata for parameters', () => {
.definitions as DefinitionsObject;

expect(defs).to.have.key('MyBody');
expect(defs.MyBody).to.deepEqual({});
expect(defs.MyBody).to.not.have.key('properties');
});

it('does not infer definition if no class metadata is present', () => {
Expand Down
165 changes: 105 additions & 60 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@ export function getJsonSchema(ctor: Function): JsonDefinition {
}
}

/**
* Gets the wrapper function of primitives string, number, and boolean
* @param type Name of type
*/
export function stringTypeToWrapper(type: string): Function {
type = type.toLowerCase();
let wrapper;
switch (type) {
case 'number': {
wrapper = Number;
break;
}
case 'string': {
wrapper = String;
break;
}
case 'boolean': {
wrapper = Boolean;
break;
}
default: {
throw new Error('Unsupported type');
}
}
return wrapper;
}

/**
* Determines whether the given constructor is a custom type or not
* @param ctor Constructor
*/
export function isComplexType(ctor: Function) {
return !includes([String, Number, Boolean, Object, Function], ctor);
}

/**
* Converts property metadata into a JSON property definition
* @param meta
*/
export function metaToJsonProperty(meta: PropertyDefinition): JsonDefinition {
let ctor = meta.type as string | Function;
let def: JsonDefinition = {};

// errors out if @property.array() is not used on a property of array
if (ctor === Array) {
throw new Error('type is defined as an array');
}

if (typeof ctor === 'string') {
ctor = stringTypeToWrapper(ctor);
}

const propDef = isComplexType(ctor)
? {$ref: `#definitions/${ctor.name}`}
: {type: ctor.name.toLowerCase()};

if (meta.array) {
def.type = 'array';
def.items = propDef;
} else {
Object.assign(def, propDef);
}

return def;
}

// NOTE(shimks) no metadata for: union, optional, nested array, any, enum,
// string literal, anonymous types, and inherited properties

Expand All @@ -57,80 +123,59 @@ export function getJsonSchema(ctor: Function): JsonDefinition {
* @param ctor Constructor of class to convert from
*/
export function modelToJsonSchema(ctor: Function): JsonDefinition {
const meta: ModelDefinition = ModelMetadataHelper.getModelMetadata(ctor);
const schema: JsonDefinition = {};
const meta: ModelDefinition | {} = ModelMetadataHelper.getModelMetadata(ctor);
const result: JsonDefinition = {};

const isComplexType = (constructor: Function) =>
!includes([String, Number, Boolean, Object], constructor);
// returns an empty object if metadata is an empty object
if (!(meta instanceof ModelDefinition)) {
return {};
}

const determinePropertyDef = (constructor: Function) =>
isComplexType(constructor)
? {$ref: `#definitions/${constructor.name}`}
: {type: constructor.name.toLowerCase()};
result.title = meta.title || ctor.name;

if (meta.description) {
result.description = meta.description;
}

for (const p in meta.properties) {
const propMeta = meta.properties[p];
let propCtor = propMeta.type;
if (typeof propCtor === 'string') {
const type = propCtor.toLowerCase();
switch (type) {
case 'number': {
propCtor = Number;
break;
}
case 'string': {
propCtor = String;
break;
}
case 'boolean': {
propCtor = Boolean;
break;
}
default: {
throw new Error('Unsupported type');
}
}
if (!meta.properties[p].type) {
continue;
}
if (propCtor && typeof propCtor === 'function') {
// errors out if @property.array() is not used on a property of array
if (propCtor === Array) {
throw new Error('type is defined as an array');
}

const propDef: JsonDefinition = determinePropertyDef(propCtor);
result.properties = result.properties || {};
result.properties[p] = result.properties[p] || {};

if (!schema.properties) {
schema.properties = {};
}
const property = result.properties[p];
const metaProperty = meta.properties[p];
const metaType = metaProperty.type;

if (propMeta.array === true) {
schema.properties[p] = {
type: 'array',
items: propDef,
};
} else {
schema.properties[p] = propDef;
}
// populating "properties" key
result.properties[p] = metaToJsonProperty(metaProperty);

if (isComplexType(propCtor)) {
const propSchema = getJsonSchema(propCtor);
// populating JSON Schema 'definitions'
if (typeof metaType === 'function' && isComplexType(metaType)) {
const propSchema = getJsonSchema(metaType);

if (propSchema && Object.keys(propSchema).length > 0) {
if (!schema.definitions) {
schema.definitions = {};
}
if (propSchema && Object.keys(propSchema).length > 0) {
result.definitions = result.definitions || {};

if (propSchema.definitions) {
for (const key in propSchema.definitions) {
schema.definitions[key] = propSchema.definitions[key];
}
delete propSchema.definitions;
// delete nested definition
if (propSchema.definitions) {
for (const key in propSchema.definitions) {
result.definitions[key] = propSchema.definitions[key];
}

schema.definitions[propCtor.name] = propSchema;
delete propSchema.definitions;
}

result.definitions[metaType.name] = propSchema;
}
}

// handling 'required' metadata
if (metaProperty.required) {
result.required = result.required || [];
result.required.push(p);
}
}
return schema;
return result;
}
Loading