Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/massimo-cli/lib/schema-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ export function buildSchemaNames (spec, takenNames = []) {
if (!schemas) return schemaNames

const keys = Object.keys(schemas)
const typeNames = new Map(keys.map(key => [key, toTypeName(key)]))
const taken = new Set([...RESERVED_TYPE_NAMES, ...takenNames])
const assigned = new Map()
const claim = key => {
const name = toTypeName(key)
const name = typeNames.get(key)
if (!taken.has(name)) {
taken.add(name)
assigned.set(key, name)
}
}
keys.filter(key => toTypeName(key) === key).forEach(claim)
keys.filter(key => toTypeName(key) !== key).forEach(claim)
keys.filter(key => typeNames.get(key) === key).forEach(claim)
keys.filter(key => typeNames.get(key) !== key).forEach(claim)
for (const key of keys) {
if (assigned.has(key)) continue
const base = `${toTypeName(key)}Schema`
const base = `${typeNames.get(key)}Schema`
let name = base
for (let i = 2; taken.has(name); i++) {
name = `${base}${i}`
Expand Down
24 changes: 24 additions & 0 deletions packages/massimo-cli/test/cli-openapi-named-schemas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ test('buildSchemaNames is deterministic and maps refs with JSON pointer escaping
equal(buildSchemaNames({}).size, 0)
})

test('buildSchemaNames preserves collision priority for normalized names', () => {
const spec = {
components: {
schemas: {
'foo-bar': {},
foo_bar: {},
'foo/bar': {},
FooBar: {},
FooBarSchema: {},
Array: {}
}
}
}

deepEqual([...buildSchemaNames(spec).entries()], [
['#/components/schemas/foo_bar', 'foo_bar'],
['#/components/schemas/FooBar', 'FooBar'],
['#/components/schemas/FooBarSchema', 'FooBarSchema'],
['#/components/schemas/foo-bar', 'FooBarSchema2'],
['#/components/schemas/foo~1bar', 'FooBarSchema3'],
['#/components/schemas/Array', 'ArraySchema']
])
})

test('--named-schemas works with --frontend', async () => {
const dir = await moveToTmpdir(after)
await execa('node', [join(import.meta.dirname, '..', 'index.js'), openAPIfile, '--name', 'movies', '--frontend', '--language', 'ts', '--named-schemas'])
Expand Down