Skip to content

Commit

Permalink
fix: should only allow override on underscore file
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotsivad committed Feb 1, 2023
1 parent 965d4bc commit 56e69b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/generator-routes/src/tree-to-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const METHODS_WITH_HANDLERS = [
'trace',
]

const getPath = (file, api) => {
let suffix = file.exports?.$path
const getPath = (file, api, suffix) => {
if (suffix) suffix = (suffix[0] === '/' ? suffix.substring(1) : suffix)
else suffix = file.key.slice(1, file.key.length - 1).join('/')
return `${api.endsWith('/') ? api : (api + '/')}${suffix}`
Expand Down Expand Up @@ -72,11 +71,21 @@ export const treeToJavascript = ({ cwd, outputDir, inputs }) => {
}
}
}
// We loop again, to resolve references.
// We loop again, to resolve references and path overwrites.
for (const { dir, api, files } of inputs) {
for (const filepath of Object.keys(files).sort()) {
if (files[filepath].key[0] !== 'paths') continue

// Here we handle path rewrites.
const originalPath = getPath(files[filepath], api)
const path = files[filepath].exports?.$path
? getPath(files[filepath], api, files[filepath].exports?.$path)
: originalPath
if (originalPath && path && originalPath !== path) {
pathToMethod[path] = pathToMethod[originalPath]
delete pathToMethod[originalPath]
}

const lastKey = files[filepath].key[files[filepath].key.length - 1]
let ref = lastKey === '_' && files[filepath].exports?.$ref
if (!ref) continue
Expand All @@ -97,7 +106,6 @@ export const treeToJavascript = ({ cwd, outputDir, inputs }) => {
continue
}

const path = getPath(files[filepath], api)
pathToMethod[path] = pathToMethod[path] || {}
// If this path doesn't have a method, use the originating references operation object:
for (const method in pathToMethod[referencePath]) {
Expand Down
9 changes: 7 additions & 2 deletions packages/generator-routes/tests/file-path-override.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ return {
ext: '@',
api: '/v1',
files: {
'paths/hello/_.@.js': {
key: [ 'paths', 'hello', '_' ],
exports: {
// The unescaped path string.
$path: '/not/hello'
},
},
'paths/hello/get.@.js': {
key: [ 'paths', 'hello', 'get' ],
exports: {
default: _ => _,
// The unescaped path string.
$path: '/not/hello'
},
},
},
Expand Down

0 comments on commit 56e69b6

Please sign in to comment.