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

Extended library with Information about "responses" w/ type information #163

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -169,6 +169,12 @@ methods:
type: boolean
isFormParameter:
type: boolean
responses:
type: object
description: Includes all defined response-codes and its JSONs schema
additionalProperties:
type: object
description: See the 'Response Object' section in the [Swagger 2.0 specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#response-object)
```

#### Custom Mustache Variables
Expand Down
6 changes: 5 additions & 1 deletion lib/codegen.js
@@ -1,5 +1,4 @@
'use strict';

var fs = require('fs');
var Mustache = require('mustache');
var beautify = require('js-beautify').js_beautify;
Expand Down Expand Up @@ -131,6 +130,11 @@ var getViewForSwagger2 = function(opts, type){
parameter.cardinality = parameter.required ? '' : '?';
method.parameters.push(parameter);
});

method.responses = op.responses;
_.forEach(method.responses, (obj, key) => {
method.responses[key].tsType = ts.convertType(obj);
});
data.methods.push(method);
});
});
Expand Down
18 changes: 18 additions & 0 deletions lib/typescript.js
Expand Up @@ -62,6 +62,24 @@ function convertType(swaggerType, swagger) {
typespec.properties.push(property);
});
}

Copy link

Choose a reason for hiding this comment

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

This _forEach should replace the above one, otherwise, it will result in duplicated typescript properties.

_.forEach(swaggerType.properties, function (propertyType, propertyName) {
var property = convertType(propertyType);
property.name = propertyName;
typespec.properties.push(property);
// see if property is required
if (Array.isArray(swaggerType.required)) {
if(swaggerType.required.indexOf(propertyName) > -1) {
property.isRequired = true;
}
else {
property.isRequired = false;
}
}
else {
property.isRequired = true;
}
});
} else {
// type unknown or unsupported... just map to 'any'...
typespec.tsType = 'any';
Expand Down
2 changes: 1 addition & 1 deletion templates/type.mustache
Expand Up @@ -4,7 +4,7 @@
<%#isRef%><%target%><%/isRef%><%!
%><%#isAtomic%><%&tsType%><%/isAtomic%><%!
%><%#isObject%>{<%#properties%>
'<%name%>': <%>type%><%/properties%>
'<%name%>'<%^isRequired%>?<%/isRequired%>: <%>type%><%/properties%>
}<%/isObject%><%!
%><%#isArray%>Array<<%#elementType%><%>type%><%/elementType%>>|<%#elementType%><%>type%><%/elementType%><%/isArray%>
<%={{ }}=%>
Expand Down