swagger-api / swagger-api/swagger-codegen
[nodejs] Question - What is the recommended pattern for continuous codegen?
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I use swagger-codegen to continuously build c# stubs using custom templates for controllers using abstract classes which are subsequently easy to override in my class implementation. This allows for nearly zero files to be excluded with .swagger-codegen-ignore. and everything works after every codegen.
What I would like to do is the same dev pattern with nodejs however it is not clear how to approach this. The Controllers implement service which in turn has the default implementation and example payload in them. I don't want to ignore any files otherwise I have to stub implementation and subsequently I would prefer not to implement in files that are overridden.
Is there an existing pattern that achieves this?
Swagger-codegen version
swagger-codegen 3.0.14
Swagger declaration file content or url
info:
version: 1.0.0
title: test
description: todo
license:
name: MIT
servers:
- url: http://todo
description: todo
paths:
/sites:
get:
summary: List all sites
operationId: listSites
tags:
- sites
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
200:
description: A paged array of sites
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Sites'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Site:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
Sites:
type: array
items:
$ref: '#/components/schemas/Site'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Command line used for generation
swagger-codegen \
generate \
-i ./test.yaml \
-l nodejs-server \
-o ../
Steps to reproduce
Example output
Controllers
'use strict';
var utils = require('../utils/writer.js');
var Sites = require('../service/SitesService');
module.exports.listSites = function listSites (req, res, next) {
var limit = req.swagger.params['limit'].value;
Sites.listSites(limit)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
Service
'use strict';
/**
* List all sites
*
* limit Integer How many items to return at one time (max 100) (optional)
* returns Sites
**/
exports.listSites = function(limit) {
return new Promise(function(resolve, reject) {
var examples = {};
examples['application/json'] = [ {
"name" : "name",
"id" : 0
}, {
"name" : "name",
"id" : 0
} ];
if (Object.keys(examples).length > 0) {
resolve(examples[Object.keys(examples)[0]]);
} else {
resolve();
}
});
}
Related issues/PRs
Suggest a fix/enhancement
Is there an abstraction overrideable pattern?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied swagger-codegen command and the generated Controllers and Service examples, then inspect how the nodejs-server generator handles custom templates and ignored files. Determine whether an overrideable continuous-generation pattern is supported, and document or demonstrate the recommended approach for preserving implementations across regeneration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nodejs, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100