Skip to content
This repository was archived by the owner on Feb 28, 2020. It is now read-only.

Commit 349cec4

Browse files
author
nhardman
authored
feat: decouple path reformatting from swaggerize
decouple the path reformatting from swaggerize
1 parent 6a6b0be commit 349cec4

File tree

12 files changed

+123
-21
lines changed

12 files changed

+123
-21
lines changed

lib/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,10 @@ exports.loadAsync = function (path, memfs) {
599599
return (isHttp ? loadHttpAsync(path) : loadFileAsync(path, memfs))
600600
.then(data => isYaml ? JSON.stringify(YAML.load(data)) : data)
601601
}
602+
603+
exports.reformatPathToSwift = function (thepath) {
604+
// take a swagger path and convert the parameters to swift format.
605+
// i.e. convert "/path/to/{param1}/{param2}" to "/path/to/:param1/:param2"
606+
var newPath = thepath.replace(/{/g, ':')
607+
return newPath.replace(/}/g, '')
608+
}

lib/sdkGenHelper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ exports.performSDKGenerationAsync = function (sdkName, sdkType, fileContent) {
6060
url: startGenURL,
6161
body: fileContent
6262
})
63-
.then(response => JSON.parse(response.body).job.id)
63+
.then(function (response) {
64+
var body = JSON.parse(response.body)
65+
if (body.job && body.job.id) {
66+
return body.job.id
67+
}
68+
throw new Error(chalk.red('SDK generation error:', response.statusCode, response.statusMessage, body.message))
69+
})
6470
.tap(generatedID => debug(`SDK generation job for ${sdkName} started with id ${generatedID}`))
6571
.then(generatedID => checkUntilFinished(generatedID))
6672

refresh/fromswagger/generatorUtils.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ function baseName (thepath) {
2121
return path.basename(thepath).split('.')[0]
2222
}
2323

24-
function convertToSwiftParameterFormat (thepath) {
25-
// take a swagger path and convert the parameters to swift format.
26-
// i.e. convert "/path/to/{param1}/{param2}" to "/path/to/:param1/:param2"
27-
var newPath = thepath.replace(/{/g, ':')
28-
return newPath.replace(/}/g, '')
29-
}
30-
3124
function resourceNameFromPath (thepath) {
3225
// grab the first valid element of a path (or partial path) and return it capitalized.
3326
var resource = thepath.match(/^\/*([^/]+)/)[1]
@@ -39,6 +32,5 @@ function getRefName (ref) {
3932
}
4033

4134
module.exports = {baseName,
42-
convertToSwiftParameterFormat,
4335
resourceNameFromPath,
4436
getRefName}

refresh/fromswagger/swaggerize.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ function ensureValidAsync (loadedSwagger) {
2929
})
3030
}
3131

32-
function parseSwagger (api) {
32+
function parseSwagger (api, pathFormatter) {
3333
debug('in parseSwagger')
3434
// walk the api, extract the schemas from the definitions, the parameters and the responses.
3535
var resources = {}
3636
var refs = []
3737
var basePath = api.basePath || undefined
38+
pathFormatter = pathFormatter || function (path) { return path }
3839

3940
Object.keys(api.paths).forEach(function (path) {
4041
var resource = genUtils.resourceNameFromPath(path)
4142

42-
debug('path:', path, 'becomes resource:', resource)
43+
debug('path:', path, 'becomes resource: "' + resource + '" with route: "' + pathFormatter(path) + '"')
4344
// for each path, walk the method verbs
4445
builderUtils.verbs.forEach(function (verb) {
4546
if (api.paths[path][verb]) {
@@ -49,7 +50,7 @@ function parseSwagger (api) {
4950

5051
debug('parsing verb:', verb)
5152
// save the method and the path in the resources list.
52-
resources[resource].push({method: verb, route: genUtils.convertToSwiftParameterFormat(path)})
53+
resources[resource].push({method: verb, route: pathFormatter(path)})
5354
// process the parameters
5455
if (api.paths[path][verb].parameters) {
5556
var parameters = api.paths[path][verb].parameters
@@ -135,14 +136,14 @@ function parseSwagger (api) {
135136
return parsed
136137
}
137138

138-
exports.parse = function (swaggerStr) {
139+
exports.parse = function (swaggerStr, pathFormatter) {
139140
debug('in parse')
140141
var loaded = JSON.parse(swaggerStr)
141142
return ensureValidAsync(loaded)
142143
.then(function () {
143144
debug('successfully validated against schema')
144145
// restore the original swagger as the call to ensureValidAsync modifies the original loaded object.
145146
loaded = JSON.parse(swaggerStr)
146-
return { loaded: loaded, parsed: parseSwagger(loaded) }
147+
return { loaded: loaded, parsed: parseSwagger(loaded, pathFormatter) }
147148
})
148149
}

refresh/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ module.exports = Generator.extend({
668668
if (!this.fromSwagger) return
669669

670670
return helpers.loadAsync(this.fromSwagger, this.fs)
671-
.then(loaded => swaggerize.parse(loaded))
671+
.then(loaded => swaggerize.parse(loaded, helpers.reformatPathToSwift))
672672
.then(response => {
673673
this.loadedApi = response.loaded
674674
this.parsedSwagger = response.parsed
@@ -705,7 +705,7 @@ module.exports = Generator.extend({
705705
return Promise.map(this.serverSwaggerFiles, file => {
706706
return helpers.loadAsync(file, this.fs)
707707
.then(loaded => {
708-
return swaggerize.parse(loaded)
708+
return swaggerize.parse(loaded, helpers.reformatPathToSwift)
709709
.then(response => {
710710
if (response.loaded.info.title === undefined) {
711711
this.env.error(chalk.red('Could not extract title from Swagger API.'))

test/integration/app/prompted_build.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var appGeneratorPath = path.join(__dirname, '../../../app')
2727
var testResourcesPath = path.join(__dirname, '../../../test/resources')
2828
var buildTimeout = 300000
2929

30+
// Require config to alter sdkgen delay between
31+
// status checks to speed up unit tests
32+
var config = require('../../../config')
33+
var sdkGenCheckDelaySaved
34+
3035
describe('Prompt and build integration tests for app generator', function () {
3136
describe('Basic application', function () {
3237
// Swift build is slow so we need to set a longer timeout for the test
@@ -100,6 +105,10 @@ describe('Prompt and build integration tests for app generator', function () {
100105
var runContext
101106
var appName = 'notes'
102107
before(function () {
108+
// alter delay between status checks to speed up unit tests
109+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
110+
config.sdkGenCheckDelay = 10000
111+
103112
// Swift build is slow so we need to set a longer timeout for the test
104113
this.timeout(buildTimeout)
105114
runContext = helpers.run(appGeneratorPath)
@@ -120,6 +129,12 @@ describe('Prompt and build integration tests for app generator', function () {
120129
return runContext.toPromise() // Get a Promise back when the generator finishes
121130
})
122131

132+
after('restore sdkgen status check delay', function () {
133+
// restore delay between status checks so integration tests
134+
// remain resilient
135+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
136+
})
137+
123138
it('compiles the application', function () {
124139
assert.file(process.cwd() + '/.build/debug/' + appName)
125140
})

test/integration/app/prompted_nobuild.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var appGeneratorPath = path.join(__dirname, '../../../app')
2727
var testResourcesPath = path.join(__dirname, '../../../test/resources')
2828
var extendedTimeout = 300000
2929

30+
// Require config to alter sdkgen delay between
31+
// status checks to speed up unit tests
32+
var config = require('../../../config')
33+
var sdkGenCheckDelaySaved
34+
3035
describe('Prompt and no build integration tests for app generator', function () {
3136
describe('Basic application', function () {
3237
this.timeout(10000) // Allow first test to be slow
@@ -222,6 +227,10 @@ describe('Prompt and no build integration tests for app generator', function ()
222227
var appName = 'notes'
223228

224229
before(function () {
230+
// alter delay between status checks to speed up unit tests
231+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
232+
config.sdkGenCheckDelay = 10000
233+
225234
runContext = helpers.run(appGeneratorPath)
226235
.withOptions({ 'skip-build': true })
227236
.withPrompts({
@@ -235,6 +244,12 @@ describe('Prompt and no build integration tests for app generator', function ()
235244
return runContext.toPromise()
236245
})
237246

247+
after('restore sdkgen status check delay', function () {
248+
// restore delay between status checks so integration tests
249+
// remain resilient
250+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
251+
})
252+
238253
describe('Static web file serving', function () {
239254
it('created public web directory', function () {
240255
assert.file('public')
@@ -321,6 +336,10 @@ describe('Prompt and no build integration tests for app generator', function ()
321336
var runContext
322337

323338
before(function () {
339+
// alter delay between status checks to speed up unit tests
340+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
341+
config.sdkGenCheckDelay = 10000
342+
324343
runContext = helpers.run(appGeneratorPath)
325344
.withOptions({ 'skip-build': true })
326345
.withPrompts({
@@ -336,6 +355,12 @@ describe('Prompt and no build integration tests for app generator', function ()
336355
return runContext.toPromise()
337356
})
338357

358+
after('restore sdkgen status check delay', function () {
359+
// restore delay between status checks so integration tests
360+
// remain resilient
361+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
362+
})
363+
339364
describe('Example endpoints', function () {
340365
it('created example endpoints', function () {
341366
assert.file(`Sources/Application/Routes/PersonsRoutes.swift`)
@@ -350,6 +375,10 @@ describe('Prompt and no build integration tests for app generator', function ()
350375
var appName = 'notes'
351376

352377
before(function () {
378+
// alter delay between status checks to speed up unit tests
379+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
380+
config.sdkGenCheckDelay = 10000
381+
353382
runContext = helpers.run(appGeneratorPath)
354383
.withOptions({ 'skip-build': true })
355384
.withPrompts({
@@ -370,6 +399,12 @@ describe('Prompt and no build integration tests for app generator', function ()
370399
return runContext.toPromise()
371400
})
372401

402+
after('restore sdkgen status check delay', function () {
403+
// restore delay between status checks so integration tests
404+
// remain resilient
405+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
406+
})
407+
373408
it('created a iOS SDK zip file', function () {
374409
assert.file(appName + '_iOS_SDK.zip')
375410
})

test/integration/app/spec_build.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ var helpers = require('yeoman-test')
2626
var appGeneratorPath = path.join(__dirname, '../../../app')
2727
var buildGeneratorPath = path.join(__dirname, '../../../build')
2828

29+
// Require config to alter sdkgen delay between
30+
// status checks to speed up unit tests
31+
var config = require('../../../config')
32+
var sdkGenCheckDelaySaved
33+
2934
describe('Spec option and build integration tests for app generator', function () {
3035
describe('A CRUD application with a cloudant service is able to build', function () {
3136
// Swift build is slow so we need to set a longer timeout for the test
@@ -66,6 +71,10 @@ describe('Spec option and build integration tests for app generator', function (
6671
}
6772

6873
before(function () {
74+
// alter delay between status checks to speed up unit tests
75+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
76+
config.sdkGenCheckDelay = 10000
77+
6978
runContext = helpers.run(appGeneratorPath)
7079
.withOptions({
7180
spec: JSON.stringify(spec)
@@ -79,6 +88,12 @@ describe('Spec option and build integration tests for app generator', function (
7988
})
8089
})
8190

91+
after('restore sdkgen status check delay', function () {
92+
// restore delay between status checks so integration tests
93+
// remain resilient
94+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
95+
})
96+
8297
it('compiles the application', function () {
8398
assert.file('.build/debug/todo')
8499
})
@@ -122,6 +137,10 @@ describe('Spec option and build integration tests for app generator', function (
122137
}
123138

124139
before(function () {
140+
// alter delay between status checks to speed up unit tests
141+
sdkGenCheckDelaySaved = config.sdkGenCheckDelay
142+
config.sdkGenCheckDelay = 10000
143+
125144
runContext = helpers.run(appGeneratorPath)
126145
.withOptions({
127146
spec: JSON.stringify(spec)
@@ -135,6 +154,12 @@ describe('Spec option and build integration tests for app generator', function (
135154
})
136155
})
137156

157+
after('restore sdkgen status check delay', function () {
158+
// restore delay between status checks so integration tests
159+
// remain resilient
160+
config.sdkGenCheckDelay = sdkGenCheckDelaySaved
161+
})
162+
138163
it('compiles the application', function () {
139164
assert.file('.build/debug/todo')
140165
})

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--timeout 10000
1+
--timeout 40000

test/unit/helpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,10 @@ describe('helpers', function () {
609609
})
610610
})
611611
})
612+
613+
describe('swagger path formatter', function () {
614+
it('convert swagger path parameters to swift format', function () {
615+
assert(helpers.reformatPathToSwift('/helper/ff/test/{p1}/{p2}') === '/helper/ff/test/:p1/:p2')
616+
})
617+
})
612618
})

0 commit comments

Comments
 (0)