-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Juggler array #1570
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
Juggler array #1570
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,10 @@ export function getJsonSchema(ctor: Function): JSONSchema { | |
| * Gets the wrapper function of primitives string, number, and boolean | ||
| * @param type Name of type | ||
| */ | ||
| export function stringTypeToWrapper(type: string): Function { | ||
| export function stringTypeToWrapper(type: string | Function): Function { | ||
| if (typeof type === 'function') { | ||
| return type; | ||
| } | ||
| type = type.toLowerCase(); | ||
| let wrapper; | ||
| switch (type) { | ||
|
|
@@ -52,6 +55,22 @@ export function stringTypeToWrapper(type: string): Function { | |
| wrapper = Boolean; | ||
| break; | ||
| } | ||
| case 'array': { | ||
| wrapper = Array; | ||
| break; | ||
| } | ||
| case 'object': { | ||
| wrapper = Object; | ||
| break; | ||
| } | ||
| case 'date': { | ||
| wrapper = Date; | ||
| break; | ||
| } | ||
| case 'buffer': { | ||
| wrapper = Buffer; | ||
| break; | ||
| } | ||
| default: { | ||
| throw new Error('Unsupported type: ' + type); | ||
| } | ||
|
|
@@ -64,40 +83,55 @@ export function stringTypeToWrapper(type: string): Function { | |
| * @param ctor Constructor | ||
| */ | ||
| export function isComplexType(ctor: Function) { | ||
| return !([String, Number, Boolean, Object, Function] as Function[]).includes( | ||
| ctor, | ||
| ); | ||
| return !([ | ||
| String, | ||
| Number, | ||
| Boolean, | ||
| Object, | ||
| Function, | ||
| Array, | ||
| ] as Function[]).includes(ctor); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a given string or constructor is array type or not | ||
| * @param type Type as string or wrapper | ||
| */ | ||
| export function isArrayType(type: string | Function) { | ||
| return type === Array || type === 'array'; | ||
| } | ||
|
|
||
| /** | ||
| * Converts property metadata into a JSON property definition | ||
| * @param meta | ||
| */ | ||
| export function metaToJsonProperty(meta: PropertyDefinition): JSONSchema { | ||
| let ctor = meta.type as string | Function; | ||
| let def: JSONSchema = {}; | ||
|
|
||
| // 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); | ||
| // tslint:disable-next-line:no-any | ||
| const propDef: JSONSchema = {}; | ||
| let result: JSONSchema; | ||
| let propertyType = meta.type as string | Function; | ||
|
|
||
| if (isArrayType(propertyType) && meta.itemType) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm...any reason why using two different ways to assert the array type?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (Array.isArray(meta.itemType)) { | ||
| throw new Error('itemType as an array is not supported'); | ||
| } | ||
| result = {type: 'array', items: propDef}; | ||
| propertyType = meta.itemType as string | Function; | ||
| } else { | ||
| result = propDef; | ||
| } | ||
|
|
||
| const propDef = isComplexType(ctor) | ||
| ? {$ref: `#/definitions/${ctor.name}`} | ||
| : {type: <JSONSchemaTypeName>ctor.name.toLowerCase()}; | ||
| propertyType = stringTypeToWrapper(propertyType); | ||
|
|
||
| if (meta.array) { | ||
| def.type = 'array'; | ||
| def.items = propDef; | ||
| if (isComplexType(propertyType)) { | ||
| Object.assign(propDef, {$ref: `#/definitions/${propertyType.name}`}); | ||
| } else { | ||
| Object.assign(def, propDef); | ||
| Object.assign(propDef, { | ||
| type: <JSONSchemaTypeName>propertyType.name.toLowerCase(), | ||
| }); | ||
| } | ||
|
|
||
| return def; | ||
| return result; | ||
| } | ||
|
|
||
| // NOTE(shimks) no metadata for: union, optional, nested array, any, enum, | ||
|
|
@@ -131,15 +165,19 @@ export function modelToJsonSchema(ctor: Function): JSONSchema { | |
| result.properties = result.properties || {}; | ||
| result.properties[p] = result.properties[p] || {}; | ||
|
|
||
| const metaProperty = meta.properties[p]; | ||
| const metaType = metaProperty.type; | ||
| const metaProperty = Object.assign({}, meta.properties[p]); | ||
|
|
||
| // populating "properties" key | ||
| result.properties[p] = metaToJsonProperty(metaProperty); | ||
|
|
||
| // populating JSON Schema 'definitions' | ||
| if (typeof metaType === 'function' && isComplexType(metaType)) { | ||
| const propSchema = getJsonSchema(metaType); | ||
| const referenceType = isArrayType(metaProperty.type as string | Function) | ||
| ? // shimks: ugly type casting; this should be replaced by logic to throw | ||
| // error if itemType/type is not a string or a function | ||
| (metaProperty.itemType as string | Function) | ||
| : (metaProperty.type as string | Function); | ||
| if (typeof referenceType === 'function' && isComplexType(referenceType)) { | ||
| const propSchema = getJsonSchema(referenceType); | ||
|
|
||
| if (propSchema && Object.keys(propSchema).length > 0) { | ||
| result.definitions = result.definitions || {}; | ||
|
|
@@ -152,7 +190,7 @@ export function modelToJsonSchema(ctor: Function): JSONSchema { | |
| delete propSchema.definitions; | ||
| } | ||
|
|
||
| result.definitions[metaType.name] = propSchema; | ||
| result.definitions[referenceType.name] = propSchema; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'string[]' ---> 'String[]' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know
string[]works but doesString[]work as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry never mind :( I mixed it with the array type in juggler.
string[]is correct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
found a
String[]on line https://github.com/strongloop/loopback-next/pull/1570/files#diff-328c8e879ea3a53d7fb50599f1c6a77eR98...I feel confused again 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they both work 🤷♂️