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

fixed missing type information on related models #622

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bower_components

# IDEs and editors
.idea
.vscode/symbols.json

# misc
.sass-cache
Expand Down
2 changes: 1 addition & 1 deletion lib/angular2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ module.exports = function generate(ctx) {
let relationType = buildRelationType( model, relation );
let defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') >= 0 ? ' = []' : '';
defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') === -1 ? ' = null' : defaultTypeValue;
output.push( ` ${relation}${isInterface ? '?' : ''}: ${relationType}${defaultTypeValue};` );
output.push( ` ${relation}${isInterface ? '?' : '?'}: ${relationType}${defaultTypeValue};` );
});
return output.join('\n');
}
Expand Down
5 changes: 4 additions & 1 deletion lib/angular2/shared/sockets/socket.browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* tslint:disable */
import * as io from 'socket.io-client';
// add this to compilerOptions in tsconfig.json:
// "allowSyntheticDefaultImports": true,
// "esModuleInterop": true
import io from 'socket.io-client';
/**
* @author Jonathan Casarrubias <twitter:@johncasarrubias> <github:@mean-expert-official>
* @module SocketBrowser
Expand Down
5 changes: 4 additions & 1 deletion lib/angular2/shared/sockets/socket.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* tslint:disable */
import * as io from 'socket.io-client';
// add this to compilerOptions in tsconfig.json:
// "allowSyntheticDefaultImports": true,
// "esModuleInterop": true
import io from 'socket.io-client';
/**
* @author Jonathan Casarrubias <twitter:@johncasarrubias> <github:@mean-expert-official>
* @module SocketNode
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ exports.buildSquema = (ctx) => {
let relationType = buildRelationType(model, relation);
let defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') >= 0 ? ' = []' : '';
defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') === -1 ? ' = null' : defaultTypeValue;
output.push(` ${relation}${isInterface ? '?' : ''}${isTyped ? ':' + relationType + defaultTypeValue + ';' : ';'}`);
output.push(` ${relation}${isInterface ? '?' : '?'}${isTyped ? ':' + relationType + defaultTypeValue + ';' : ';'}`);
});
return output.join('\n');
}
Expand Down
26 changes: 23 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.describeModels = function describeModels(app) {
c.sharedClass.ctor.settings
);
c.sharedClass.ctor.settings.sdk = Object.assign(
{enabled: true},
{ enabled: true },
c.sharedClass.ctor.settings.sdk
);
// Tell SDK to blacklist specific methods
Expand Down Expand Up @@ -84,15 +84,15 @@ exports.describeModels = function describeModels(app) {
method.accepts.forEach((param, index, arr) => {
if (loaded[param.arg]) {
arr.splice(index, 1);
} else  {
} else {
loaded[param.arg] = true;
}
});
}
if (!method.accepts) return;
// Filter out parameters that are generated from the incoming request,
// or generated by functions that use those resources.
method.accepts = method.accepts.filter(function(arg) {
method.accepts = method.accepts.filter(function (arg) {
if (!arg.http) return true;
// Don't show derived arguments.
if (typeof arg.http === 'function') return false;
Expand Down Expand Up @@ -125,9 +125,29 @@ exports.describeModels = function describeModels(app) {

buildScopes(models);

completeTargetClass(models)

return models;
};

// adds the targetClass to all relations, also those that are not
// used by a scope method. Otherwise type information will be missing
// on related models.
function completeTargetClass(models) {
for (var modelName in models) {
// Skip Account since otherwise Account Service gets a duplicate import statement
// for AccessToken
if (modelName !== 'PubAccount') {
var modelClass = models[modelName];
var relations = modelClass.sharedClass.ctor.relations;
Object.keys(relations).forEach(key => {
var relation = relations[key];
if (!relation.targetClass) relation.targetClass = relation.modelTo.name;
})
}
}
}

var SCOPE_METHOD_REGEX = /^prototype.__([^_]+)__(.+)$/;

function buildScopes(models) {
Expand Down
5 changes: 4 additions & 1 deletion tests/ng2web/src/app/room-ngrx/room-ngrx.list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { Observable } from 'rxjs/Observable';
import { LoopBackConfig } from '../shared/sdk/lb.config';
LoopBackConfig.setBaseURL('http://localhost:3000');

import * as io from 'socket.io-client';
// add this to compilerOptions in tsconfig.json:
// "allowSyntheticDefaultImports": true,
// "esModuleInterop": true
import io from 'socket.io-client';

import { Orm } from '../shared/sdk/orm';

Expand Down
6 changes: 5 additions & 1 deletion tests/ng2web/src/app/room/room.list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { Subscription } from 'rxjs';
import { LoopBackConfig } from '../shared/sdk/lb.config';
LoopBackConfig.setBaseURL('http://localhost:3000');

import * as io from 'socket.io-client';
// add this to compilerOptions in tsconfig.json:
// "allowSyntheticDefaultImports": true,
// "esModuleInterop": true
import io from 'socket.io-client';


var i = 0;

Expand Down