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

parse definitions from Swagger #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

eburi
Copy link

@eburi eburi commented Jul 13, 2016

Added simple code to also parse the definitions found in swagger-files.

Sample template to output nothing but the definitions:

{{#definitions}}
interface {{name}} {{>type}}
{{/definitions}}
        var swaggerJson = JSON.parse(response);
        var typeTemplate = fs.readFileSync(__dirname + '/templates/typescript-definitions-only.mustache', 'utf-8');
        var opts = {
            swagger: swaggerJson,
            template: {
                class: typeTemplate
            }
        };
        var tsDefinitions = CodeGen.getTypescriptCode(opts);
        var defFile = fs.createWriteStream('./myDefinitions.d.ts');
        defFile.write(tsDefinitions);

The change in lib/typescript.js is for a special case I encountered with a swagger file where a parameter was defined as an array but misses the items-property describing the elements of the array. So I added this line to sanitize the input and default to a safe option instead of crashing when it tries to call 'hasOwnProperty' of undefined.

@wcandillon
Copy link
Owner

@eburi Can you provide some tests/examples?

@no-more
Copy link

no-more commented Aug 28, 2016

Please merge this feature.

Thanks a lot.

@wcandillon
Copy link
Owner

@no-more @eburi Could you provide me an example on how to use this feature?

@no-more
Copy link

no-more commented Aug 30, 2016

I haven't personally used it. I've just started a project and I faced this need.
I think he provided some explanation on how to use this in this post #115

@greuze
Copy link

greuze commented Oct 19, 2016

I am facing the same problem, my swagger spec have several type definitions, that are not generated, and as they are passed as parameters, there are compilation errors.

This pull request solves this problem (the types are generated), but there is a new minor problem. Types that are directly a string, are translated to an incorrect interface (other types are translated correctly):

interface user_id string

This causes a compilation error.

@greuze
Copy link

greuze commented Oct 19, 2016

To try the code out, a template with definitions block must be specified:

{{#definitions}}
interface {{name}} {{>type}}
{{/definitions}}

I copied templates/typescript-class.mustache and put the block after the import * as request from "superagent"; (line 5). Specifying method template and type template is not required, as they will use default template. For example:

var opts = {
    swagger: swaggerJson,
    template: {
        class: typeTemplate
    }
};
var tsSourceCode = CodeGen.getTypescriptCode(opts);

With current code, the definition block is however not generated, but with pull request code, it is.

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

4 participants