-
Notifications
You must be signed in to change notification settings - Fork 99
Update to Swagger 1.2. Fixes #11, #8, #7. #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
56003f0
Load swagger ui from `swagger-ui` package instead.
eb31787
Swagger 1.2 compatability. Moved strong-remoting/ext/swagger to this …
2f8506c
Fix api resource path and type ref to models.
67bb10b
Some swagger 1.2 migration cleanup.
d34304a
Make sure body parameter is shown.
4c0ce42
Refactoring swagger 1.2 rework.
a4a36f5
Refactor key translations between LDL & Swagger.
5f22982
Allow easy setting of accessToken in explorer UI.
STRML a6afe13
Restore existing styles.
STRML 19c3fe3
Fix missing strong-remoting devDependency.
STRML 70dddef
Use express routes instead of modifying remoting.
STRML 77f0167
LDL to Swagger fixes & extensions.
STRML 3ce35e1
Refactor route-helper & add tests.
STRML 781ac2b
Rename translateKeys to translateDataTypeKeys.
STRML 75713f1
Add url-join so path.join() doesn't break windows
STRML cbf768f
Remove peerDependencies, use express directly.
STRML 34b3983
Remove swagger.test.js license
STRML 6224243
Remove preMiddleware.
STRML 75ba058
More consise type tests
STRML 4b3785c
Simplify `accepts` and `returns` hacks.
STRML a857fe5
Remove forgotten TODO.
STRML 5c130a4
Fix debug namespace, express version.
STRML cefbdb3
Fix resources if the explorer is at a deep path.
STRML fcd1926
s/accessToken/access_token in authorization key name
STRML File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "node": true, | ||
| "camelcase" : true, | ||
| "eqnull" : true, | ||
| "indent": 2, | ||
| "undef": true, | ||
| "quotmark": "single", | ||
| "maxlen": 80, | ||
| "trailing": true, | ||
| "newcap": true, | ||
| "nonew": true, | ||
| "undef": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,19 @@ | ||
| var loopback = require('loopback'); | ||
| var app = loopback(); | ||
| var explorer = require('../'); | ||
| var port = 3000; | ||
|
|
||
| var Product = loopback.Model.extend('product'); | ||
| var Product = loopback.Model.extend('product', { | ||
| foo: {type: 'string', required: true}, | ||
| bar: 'string', | ||
| aNum: {type: 'number', min: 1, max: 10, required: true, default: 5} | ||
| }); | ||
| Product.attachTo(loopback.memory()); | ||
| app.model(Product); | ||
|
|
||
| app.use(loopback.rest()); | ||
| app.use('/explorer', explorer(app)); | ||
| var apiPath = '/api'; | ||
| app.use('/explorer', explorer(app, {basePath: apiPath})); | ||
| app.use(apiPath, loopback.rest()); | ||
| console.log('Explorer mounted at localhost:' + port + '/explorer'); | ||
|
|
||
| app.listen(3000); | ||
| app.listen(port); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| var modelHelper = require('./model-helper'); | ||
| var urlJoin = require('./url-join'); | ||
|
|
||
| /** | ||
| * Export the classHelper singleton. | ||
| */ | ||
| var classHelper = module.exports = { | ||
| /** | ||
| * Given a remoting class, generate an API doc. | ||
| * See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#52-api-declaration | ||
| * @param {Class} aClass Strong Remoting class. | ||
| * @param {Object} opts Options (passed from Swagger(remotes, options)) | ||
| * @param {String} opts.version API Version. | ||
| * @param {String} opts.swaggerVersion Swagger version. | ||
| * @param {String} opts.basePath Basepath (usually e.g. http://localhost:3000). | ||
| * @param {String} opts.resourcePath Resource path (usually /swagger/resources). | ||
| * @return {Object} API Declaration. | ||
| */ | ||
| generateAPIDoc: function(aClass, opts) { | ||
| return { | ||
| apiVersion: opts.version, | ||
| swaggerVersion: opts.swaggerVersion, | ||
| basePath: opts.basePath, | ||
| resourcePath: urlJoin('/', opts.resourcePath), | ||
| apis: [], | ||
| models: modelHelper.generateModelDefinition(aClass) | ||
| }; | ||
| }, | ||
| /** | ||
| * Given a remoting class, generate a reference to an API declaration. | ||
| * This is meant for insertion into the Resource declaration. | ||
| * See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#512-resource-object | ||
| * @param {Class} aClass Strong Remoting class. | ||
| * @return {Object} API declaration reference. | ||
| */ | ||
| generateResourceDocAPIEntry: function(aClass) { | ||
| return { | ||
| path: aClass.http.path, | ||
| description: aClass.ctor.sharedCtor && aClass.ctor.sharedCtor.description | ||
| }; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| var _cloneDeep = require('lodash.clonedeep'); | ||
| var translateDataTypeKeys = require('./translate-data-type-keys'); | ||
|
|
||
| /** | ||
| * Export the modelHelper singleton. | ||
| */ | ||
| var modelHelper = module.exports = { | ||
| /** | ||
| * Given a class (from remotes.classes()), generate a model definition. | ||
| * This is used to generate the schema at the top of many endpoints. | ||
| * @param {Class} class Remote class. | ||
| * @return {Object} Associated model definition. | ||
| */ | ||
| generateModelDefinition: function generateModelDefinition(aClass) { | ||
| var def = aClass.ctor.definition; | ||
| var name = def.name; | ||
|
|
||
| var required = []; | ||
| // Don't modify original properties. | ||
| var properties = _cloneDeep(def.properties); | ||
|
|
||
| // Iterate through each property in the model definition. | ||
| // Types may be defined as constructors (e.g. String, Date, etc.), | ||
| // or as strings; getPropType() will take care of the conversion. | ||
| // See more on types: | ||
| // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives | ||
| Object.keys(properties).forEach(function(key) { | ||
| var prop = properties[key]; | ||
|
|
||
| // Eke a type out of the constructors we were passed. | ||
| prop = modelHelper.LDLPropToSwaggerDataType(prop); | ||
|
|
||
| // Required props sit in a per-model array. | ||
| if (prop.required || (prop.id && !prop.generated)) { | ||
| required.push(key); | ||
| } | ||
|
|
||
| // Change mismatched keys. | ||
| prop = translateDataTypeKeys(prop); | ||
|
|
||
| // Assign this back to the properties object. | ||
| properties[key] = prop; | ||
| }); | ||
|
|
||
| var out = {}; | ||
| out[name] = { | ||
| id: name, | ||
| properties: properties, | ||
| required: required | ||
| }; | ||
| return out; | ||
| }, | ||
|
|
||
| /** | ||
| * Given a propType (which may be a function, string, or array), | ||
| * get a string type. | ||
| * @param {*} propType Prop type description. | ||
| * @return {String} Prop type string. | ||
| */ | ||
| getPropType: function getPropType(propType) { | ||
| if (typeof propType === 'function') { | ||
| propType = propType.name.toLowerCase(); | ||
| } else if(Array.isArray(propType)) { | ||
| propType = 'array'; | ||
| } | ||
| return propType; | ||
| }, | ||
|
|
||
| // Converts a prop defined with the LDL spec to one conforming to the | ||
| // Swagger spec. | ||
| // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives | ||
| LDLPropToSwaggerDataType: function LDLPropToSwaggerDataType(prop) { | ||
| var out = _cloneDeep(prop); | ||
| out.type = modelHelper.getPropType(out.type); | ||
|
|
||
| if (out.type === 'array') { | ||
| var arrayProp = prop.type[0]; | ||
| if (!arrayProp.type) arrayProp = {type: arrayProp}; | ||
| out.items = modelHelper.LDLPropToSwaggerDataType(arrayProp); | ||
| } | ||
|
|
||
| if (out.type === 'date') { | ||
| out.type = 'string'; | ||
| out.format = 'date'; | ||
| } else if (out.type === 'buffer') { | ||
| out.type = 'string'; | ||
| out.format = 'byte'; | ||
| } else if (out.type === 'number') { | ||
| out.format = 'double'; // Since all JS numbers are doubles | ||
| } | ||
| return out; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think
id:trueproperty are automatically required, they are auto-generated in many cases.@raymondfeng can you confirm?