Skip to content

Commit

Permalink
Correct import when refs are using . syntax(creating subfolders) (#796)
Browse files Browse the repository at this point in the history
* fix: remove use of ts namespace

* chore: update examples

* fix: A schema name with `pet.Pets` will be moved inside of the folder `src/gen/pet/pets/ts` instead of `src/gen/petPets.ts`

* chore: update examples

* chore: fix examples

* chore: fix plugin zod
  • Loading branch information
stijnvanhulle authored Jan 21, 2024
1 parent 3e46f03 commit bf1e521
Show file tree
Hide file tree
Showing 42 changed files with 181 additions and 96 deletions.
6 changes: 6 additions & 0 deletions .changeset/perfect-trees-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kubb/swagger-ts": patch
"@kubb/core": patch
---

A schema name with `pet.Pets` will be moved inside of the folder `src/gen/pet/pets/ts` instead of `src/gen/petPets.ts`
6 changes: 3 additions & 3 deletions examples/advanced/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ components:
example: 1
xml:
name: user
Tag:
tag.Tag:
type: object
properties:
id:
Expand Down Expand Up @@ -851,7 +851,7 @@ components:
xml:
wrapped: true
items:
$ref: "#/components/schemas/Tag"
$ref: "#/components/schemas/tag.Tag"
status:
type: string
description: pet status in the store
Expand Down Expand Up @@ -889,7 +889,7 @@ components:
xml:
wrapped: true
items:
$ref: "#/components/schemas/Tag"
$ref: "#/components/schemas/tag.Tag"
status:
type: string
description: pet status in the store
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/mocks/createAddPetRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCategory } from './createCategory'
import { createTag } from './createTag'
import { createTagTag } from './tag/createTag'
import { faker } from '@faker-js/faker'
import type { AddPetRequest } from '../models/ts/AddPetRequest'

Expand All @@ -9,7 +9,7 @@ export function createAddPetRequest(): NonNullable<AddPetRequest> {
'name': faker.string.alpha(),
'category': createCategory(),
'photoUrls': faker.helpers.arrayElements([faker.string.alpha()]) as any,
'tags': faker.helpers.arrayElements([createTag()]) as any,
'tags': faker.helpers.arrayElements([createTagTag()]) as any,
'status': faker.helpers.arrayElement<any>([`available`, `pending`, `sold`]),
}
}
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/mocks/createPet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCategory } from './createCategory'
import { createTag } from './createTag'
import { createTagTag } from './tag/createTag'
import { faker } from '@faker-js/faker'
import type { Pet } from '../models/ts/Pet'

Expand All @@ -9,7 +9,7 @@ export function createPet(): NonNullable<Pet> {
'name': faker.string.alpha(),
'category': createCategory(),
'photoUrls': faker.helpers.arrayElements([faker.string.alpha()]) as any,
'tags': faker.helpers.arrayElements([createTag()]) as any,
'tags': faker.helpers.arrayElements([createTagTag()]) as any,
'status': faker.helpers.arrayElement<any>([`available`, `pending`, `sold`]),
}
}
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export * from './createCustomer'
export * from './createOrder'
export * from './createPet'
export * from './createPetNotFound'
export * from './createTag'
export * from './createUser'
export * from './createUserArray'
export * from './tag/index'
export * as petsMocks from './petsController/index'
export * as petMocks from './petController/index'
export * as userMocks from './userController/index'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faker } from '@faker-js/faker'
import type { Tag } from '../models/ts/Tag'
import type { TagTag } from '../../models/ts/tag/Tag'

export function createTag(): NonNullable<Tag> {
export function createTagTag(): NonNullable<TagTag> {
return { 'id': faker.number.float({}), 'name': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/advanced/src/gen/mocks/tag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './createTag'
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/models/ts/AddPetRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Category } from './Category'
import type { Tag } from './Tag'
import type { TagTag } from './tag/Tag'

export const AddPetRequestStatus = {
'available': 'available',
Expand All @@ -26,7 +26,7 @@ export type AddPetRequest = {
/**
* @type array | undefined
*/
tags?: Tag[]
tags?: TagTag[]
/**
* @description pet status in the store
* @type string | undefined
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/models/ts/Pet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Category } from './Category'
import type { Tag } from './Tag'
import type { TagTag } from './tag/Tag'

export const PetStatus = {
'available': 'available',
Expand All @@ -26,7 +26,7 @@ export type Pet = {
/**
* @type array | undefined
*/
tags?: Tag[]
tags?: TagTag[]
/**
* @description pet status in the store
* @type string | undefined
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/models/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './petController/index.js'
export * from './PetNotFound.js'
export * from './petsController/index.js'
export * from './storeController/index.js'
export * from './Tag.js'
export * from './tag/index.js'
export * from './User.js'
export * from './UserArray.js'
export * from './userController/index.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Tag = {
export type TagTag = {
/**
* @type integer | undefined int64
*/
Expand Down
1 change: 1 addition & 0 deletions examples/advanced/src/gen/models/ts/tag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Tag.js'
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas/AddPetRequest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
},
"status": { "type": "string", "description": "pet status in the store", "enum": ["available", "pending", "sold"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas/Pet.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
},
"status": { "type": "string", "description": "pet status in the store", "enum": ["available", "pending", "sold"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas2/AddPetRequest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
},
"status": { "type": "string", "description": "pet status in the store", "enum": ["available", "pending", "sold"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas2/Pet.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
},
"status": { "type": "string", "description": "pet status in the store", "enum": ["available", "pending", "sold"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"type": "object",
"properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" } },
"xml": { "name": "tag" },
"x-readme-ref-name": "Tag"
"x-readme-ref-name": "tag.Tag"
}
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/zod/addPetRequestSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { categorySchema } from './categorySchema'
import { tagSchema } from './tagSchema'
import { tagTagSchema } from './tag/tagSchema'
import { z } from 'zod'
import type { AddPetRequest } from '../models/ts/AddPetRequest'

Expand All @@ -8,6 +8,6 @@ export const addPetRequestSchema: z.ZodType<AddPetRequest> = z.object({
'name': z.string(),
'category': z.lazy(() => categorySchema).optional(),
'photoUrls': z.array(z.string()),
'tags': z.array(z.lazy(() => tagSchema)).optional(),
'tags': z.array(z.lazy(() => tagTagSchema)).optional(),
'status': z.enum([`available`, `pending`, `sold`]).describe(`pet status in the store`).optional(),
})
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * as petSchemas from './petController/index'
export * from './petNotFoundSchema'
export * from './petSchema'
export * as petsSchemas from './petsController/index'
export * from './tagSchema'
export * from './tag/index'
export * from './userArraySchema'
export * as userSchemas from './userController/index'
export * from './userSchema'
4 changes: 2 additions & 2 deletions examples/advanced/src/gen/zod/petSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { categorySchema } from './categorySchema'
import { tagSchema } from './tagSchema'
import { tagTagSchema } from './tag/tagSchema'
import { z } from 'zod'
import type { Pet } from '../models/ts/Pet'

Expand All @@ -8,6 +8,6 @@ export const petSchema: z.ZodType<Pet> = z.object({
'name': z.string(),
'category': z.lazy(() => categorySchema).optional(),
'photoUrls': z.array(z.string()),
'tags': z.array(z.lazy(() => tagSchema)).optional(),
'tags': z.array(z.lazy(() => tagTagSchema)).optional(),
'status': z.enum([`available`, `pending`, `sold`]).describe(`pet status in the store`).optional(),
})
1 change: 1 addition & 0 deletions examples/advanced/src/gen/zod/tag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tagSchema'
4 changes: 4 additions & 0 deletions examples/advanced/src/gen/zod/tag/tagSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { z } from 'zod'
import type { TagTag } from '../../models/ts/tag/Tag'

export const tagTagSchema: z.ZodType<TagTag> = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
4 changes: 0 additions & 4 deletions examples/advanced/src/gen/zod/tagSchema.ts

This file was deleted.

26 changes: 26 additions & 0 deletions packages/core/src/transformers/casing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { camelCase, pascalCase, pathCase } from './casing.ts'

describe('casing', () => {
test('camelCase', () => {
expect(camelCase('pet pet')).toBe('petPet')
expect(camelCase('pet.Pet', { isFile: true })).toBe('pet/pet')
expect(camelCase('create tag.tag', { isFile: true })).toBe('createTag/tag')
expect(camelCase('tag.tag', { isFile: true, prefix: 'create' })).toBe('tag/createTag')
expect(camelCase('tag.tag', { isFile: true, suffix: 'schema' })).toBe('tag/tagSchema')
})
test('pascalCase', () => {
expect(pascalCase('pet pet')).toBe('PetPet')
expect(pascalCase('pet.Pet', { isFile: true })).toBe('pet/Pet')
expect(pascalCase('create tag.tag', { isFile: true })).toBe('createTag/Tag')
expect(pascalCase('tag.tag', { isFile: true, prefix: 'create' })).toBe('tag/CreateTag')
expect(pascalCase('tag.tag', { isFile: true, suffix: 'schema' })).toBe('tag/TagSchema')
})

test('pathCase', () => {
expect(pathCase('pet pet')).toBe('petpet')
expect(pathCase('pet.Pet', { isFile: true })).toBe('pet/pet')
expect(pathCase('create tag.tag', { isFile: true })).toBe('createTag/tag')
expect(pathCase('tag.tag', { isFile: true, prefix: 'create' })).toBe('tag/createtag')
expect(pathCase('tag.tag', { isFile: true, suffix: 'schema' })).toBe('tag/tagschema')
})
})
38 changes: 33 additions & 5 deletions packages/core/src/transformers/casing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
import { camelCase as changeCaseCamel, pascalCase as changePascalCase } from 'change-case'
import { camelCase as changeCamelCase, pascalCase as changePascalCase, pathCase as changePathCase } from 'change-case'

export function camelCase(text: string): string {
return changeCaseCamel(text, { delimiter: '', mergeAmbiguousCharacters: true })
type Options = {
/**
* When set it will replace all `.` with `/`.
*/
isFile?: boolean
prefix?: string
suffix?: string
}

export function pascalCase(text: string): string {
return changePascalCase(text, { delimiter: '', mergeAmbiguousCharacters: true })
export function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
if (isFile) {
const splitArray = text.split('.')
return splitArray.map((item, i) => i === splitArray.length - 1 ? camelCase(item, { prefix, suffix }) : camelCase(item)).join('/')
}

return changeCamelCase(`${prefix} ${text} ${suffix}`, { delimiter: '', mergeAmbiguousCharacters: true })
}

export function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
if (isFile) {
const splitArray = text.split('.')
return splitArray.map((item, i) => i === splitArray.length - 1 ? pascalCase(item, { prefix, suffix }) : camelCase(item)).join('/')
}

return changePascalCase(`${prefix} ${text} ${suffix}`, { delimiter: '', mergeAmbiguousCharacters: true })
}

export function pathCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
if (isFile) {
const splitArray = text.split('.')
return splitArray.map((item, i) => i === splitArray.length - 1 ? pathCase(item, { prefix, suffix }) : camelCase(item)).join('/')
}

return changePathCase(`${prefix} ${text} ${suffix}`, { delimiter: '' })
}
5 changes: 3 additions & 2 deletions packages/core/src/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelCase, pascalCase } from './casing.ts'
import { camelCase, pascalCase, pathCase } from './casing.ts'
import { combineCodes } from './combineCodes.ts'
import { createJSDocBlockText } from './createJSDocBlockText.ts'
import { escape, jsStringEscape } from './escape.ts'
Expand All @@ -8,7 +8,7 @@ import { searchAndReplace } from './searchAndReplace.ts'
import { transformReservedWord } from './transformReservedWord.ts'
import { trim, trimExtName } from './trim.ts'

export { camelCase, pascalCase } from './casing.ts'
export { camelCase, pascalCase, pathCase } from './casing.ts'
export { combineCodes } from './combineCodes.ts'
export { createJSDocBlockText } from './createJSDocBlockText.ts'
export { escape, jsStringEscape } from './escape.ts'
Expand All @@ -33,4 +33,5 @@ export default {
},
camelCase,
pascalCase,
pathCase,
} as const
2 changes: 1 addition & 1 deletion packages/swagger-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
return path.resolve(root, output.path, baseName)
},
resolveName(name, type) {
const resolvedName = camelCase(name)
const resolvedName = camelCase(name, { isFile: type === 'file' })

if (type) {
return transformers?.name?.(resolvedName, type) || resolvedName
Expand Down
31 changes: 18 additions & 13 deletions packages/swagger-faker/src/FakerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,25 @@ export class FakerGenerator extends Generator<PluginOptions['resolvedOptions'],
})
// hack to add typescript imports
if (typeName) {
const ref = {
propertyName: typeName,
originalName: baseName,
}
const path = this.context.pluginManager.resolvePath({
baseName: operationName || typeName,
const typeFileName = this.context.pluginManager.resolveName({ name: baseName, pluginKey: swaggerTypeScriptPluginKey, type: 'file' })

const typePath = this.context.pluginManager.resolvePath({
baseName: operationName || typeFileName,
pluginKey: swaggerTypeScriptPluginKey,
options: { tag: operation?.getTags()[0]?.name },
})

this.imports.push({
ref,
path: path || '',
isTypeOnly: true,
})
if (typePath) {
this.imports.push({
ref: {
propertyName: typeName,
originalName: baseName,
pluginKey: swaggerTypeScriptPluginKey,
},
path: typePath,
isTypeOnly: true,
})
}
}

texts.push(fakerOutput)
Expand Down Expand Up @@ -141,13 +145,14 @@ export class FakerGenerator extends Generator<PluginOptions['resolvedOptions'],
const originalName = getUniqueName($ref.replace(/.+\//, ''), this.#usedAliasNames)
const propertyName = this.context.pluginManager.resolveName({ name: originalName, pluginKey, type: 'function' })

const fileName = this.context.pluginManager.resolveName({ name: originalName, pluginKey, type: 'file' })
const path = this.context.pluginManager.resolvePath({ baseName: fileName, pluginKey })

ref = this.refs[$ref] = {
propertyName,
originalName,
}

const path = this.context.pluginManager.resolvePath({ baseName: propertyName, pluginKey })

this.imports.push({
ref,
path: path || '',
Expand Down
Loading

1 comment on commit bf1e521

@vercel
Copy link

@vercel vercel bot commented on bf1e521 Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kubb – ./

kubb-git-main-kubb.vercel.app
www.kubb.dev
kubb-kubb.vercel.app
kubb.dev

Please sign in to comment.