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

Implement parser/unmarshaller #295

Merged
merged 1 commit into from
May 25, 2017
Merged

Implement parser/unmarshaller #295

merged 1 commit into from
May 25, 2017

Conversation

rashmihunt
Copy link
Contributor

  • New parser/unmarshaller function (yet to delete the same logic/functions from SwaggerRouter)
  • This code assumes router is already created which has mounted controllers and created endpoints prior to calling parser() method. (We still need to create the router() function with just this logic.. In master branch code, everything is glued together in SwaggerRouter and Application.ts).
  • See comments at the beginning of the method in the file.
  • Parser method only unmarshalls the request and returns back the method arguments for the given endpoint.

@bajtos @raymondfeng Feel free to provide initial feedback.

if (!match) {
debug(' -> next (path mismatch)');
return [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, these two if checks belong to the router. The parser should receive only the Endpoint matching the request.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me!

pathParams[key.name] = match[matchIndex];
}

const operationName = endPoint._spec['x-operation-name'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operationName looks unused to me, can you remove it please?

const debug = require('debug')('loopback:request-parser');
import * as Promise from 'bluebird';
import * as url from 'url';
import * as pathToRegexp from 'path-to-regexp';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these imports are unused, e.g. pathToRegexp. It would be great to clean them up.

status?: number;
}

function createHttpError(statusCode: number, message: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have discussed elsewhere (sorry, don't have a link), creating an HTTP error object is so frequent case that LoopBack should provide a public API for that. One option is to leverage http-errors module, as we are already doing in CodeHub's UserController.

Having said that, it's probably best to leave this improvement out of this initial pull request.

// As per the earlier design discussion, parser() was suppose to take request and swagger spec
// as arguments. However, this parsing/unmarshalling logic needs an instance of an Endpoint which is already created
// in the router prior to parser(..) being called. Router would have mounted controllers and created endpoints
// before parser() gets called. Hence Swagger spec is not needed here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the only reason why we need the Endpoint instance is to access the arguments mapped to path parameters. IMO, this is something that should be handled by the router.

The signature of the parser function then can become something like the following:

export function parser(
  request: Request, pathParameters: StringToStringMap, spec: OperationObject)
  : OperationArgs

@bajtos
Copy link
Member

bajtos commented May 24, 2017

@rashmihunt I added one more commit where I addressed my comments and integrated the new parse with SwaggerRouter. PTAL.

@rashmihunt
Copy link
Contributor Author

rashmihunt commented May 24, 2017

@bajtos LGTM. I am ok with you going ahead and merging this PR. This way we can do smaller, step by step decompose changes into master. I will work on unmarshaller/write next.

@rashmihunt rashmihunt changed the title Work In Progress - parser/unmarshaller Implement parser/unmarshaller May 24, 2017
@bajtos
Copy link
Member

bajtos commented May 25, 2017

@rashmihunt sounds good. I have added at least few basic unit tests to make it easier to follow TDD in the future - see 4f506c9

@bajtos
Copy link
Member

bajtos commented May 25, 2017

I have also renamed the function parser to parseOperationArgs to make the intent clearer, especially when the method ends up in an error stack trace.

@bajtos bajtos merged commit 3e25c78 into master May 25, 2017
@bajtos bajtos deleted the decompose branch May 25, 2017 14:11
@bajtos bajtos removed the review label May 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants