Skip to content

Commit

Permalink
fix: remove duplicated keys when using allOf and applying required …
Browse files Browse the repository at this point in the history
…on fields
  • Loading branch information
stijnvanhulle committed Jan 21, 2025
1 parent 41fb25f commit f5bae0d
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-sloths-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-oas": patch
---

remove duplicated keys when using `allOf` and applying required on fields
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ title: Changelog

## 3.5.2
- [`plugin-faker`](/plugins/plugin-faker): `faker.number.float` with default min `Number.MIN_VALUE` and max set to `Number.MAX_VALUE`.
- [`plugin-oas`](/plugins/plugin-oas): remove duplicated keys when using `allOf` and applying required on fields

## 3.5.1
- [`core`](/plugins/core): build of `@kubb/core` with correct types
Expand Down
1 change: 1 addition & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"build": "tsup",
"clean": "npx rimraf ./dist",
"generate": "kubb generate",
"generate:debug": "NODE_OPTIONS='--inspect-brk' kubb generate",
"start": "tsup --watch",
"test": "vitest",
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
Expand Down
14 changes: 12 additions & 2 deletions examples/typescript/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,10 @@ components:
Address:
type: object
properties:
street:
streetName:
type: string
streetNumber:
type: string
example: 437 Lytton
city:
type: string
example: Palo Alto
Expand Down Expand Up @@ -832,6 +833,15 @@ components:
readOnly: true
bark:
type: string
FullAddress:
properties:
streetName:
type: string
allOf:
- $ref: "#/components/schemas/Address"
required:
- streetName
- streetNumber
AddPetRequest:
required:
- name
Expand Down
1 change: 1 addition & 0 deletions examples/typescript/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type {
FindPetsByTagsQueryResponse,
FindPetsByTagsQuery,
} from './ts/models/FindPetsByTags.ts'
export type { FullAddress } from './ts/models/FullAddress.ts'
export type { GetInventory200, GetInventoryQueryResponse, GetInventoryQuery } from './ts/models/GetInventory.ts'
export type {
GetOrderByIdPathParams,
Expand Down
18 changes: 17 additions & 1 deletion examples/typescript/src/gen/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export interface Address {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down Expand Up @@ -240,6 +244,18 @@ export interface Dog {
bark?: string
}

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}

export enum AddPetRequestStatusEnum {
available = 'available',
pending = 'pending',
Expand Down
18 changes: 17 additions & 1 deletion examples/typescript/src/gen/modelsConst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export type Address = {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down Expand Up @@ -252,6 +256,18 @@ export type Dog = {
bark?: string
}

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}

export const addPetRequestStatusEnum = {
available: 'available',
pending: 'pending',
Expand Down
18 changes: 17 additions & 1 deletion examples/typescript/src/gen/modelsConstEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export type Address = {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down Expand Up @@ -240,6 +244,18 @@ export type Dog = {
bark?: string
}

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}

export enum AddPetRequestStatusEnum {
available = 'available',
pending = 'pending',
Expand Down
18 changes: 17 additions & 1 deletion examples/typescript/src/gen/modelsLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export type Address = {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down Expand Up @@ -221,6 +225,18 @@ export type Dog = {
bark?: string
}

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}

export type AddPetRequestStatusEnum = 'available' | 'pending' | 'sold'

export type AddPetRequest = {
Expand Down
18 changes: 17 additions & 1 deletion examples/typescript/src/gen/modelsPascalConst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export type Address = {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down Expand Up @@ -252,6 +256,18 @@ export type Dog = {
bark?: string
}

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}

export const addPetRequestStatusEnum = {
available: 'available',
pending: 'pending',
Expand Down
3 changes: 2 additions & 1 deletion examples/typescript/src/gen/schemas/Address.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"type": "object",
"properties": {
"street": { "type": "string", "example": "437 Lytton" },
"streetName": { "type": "string" },
"streetNumber": { "type": "string" },
"city": { "type": "string", "example": "Palo Alto" },
"state": { "type": "string", "example": "CA" },
"zip": { "type": "string", "example": "94301" }
Expand Down
3 changes: 2 additions & 1 deletion examples/typescript/src/gen/schemas/Customer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"items": {
"type": "object",
"properties": {
"street": { "type": "string", "example": "437 Lytton" },
"streetName": { "type": "string" },
"streetNumber": { "type": "string" },
"city": { "type": "string", "example": "Palo Alto" },
"state": { "type": "string", "example": "CA" },
"zip": { "type": "string", "example": "94301" }
Expand Down
19 changes: 19 additions & 0 deletions examples/typescript/src/gen/schemas/fullAddress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"properties": { "streetName": { "type": "string" } },
"allOf": [
{
"type": "object",
"properties": {
"streetName": { "type": "string" },
"streetNumber": { "type": "string" },
"city": { "type": "string", "example": "Palo Alto" },
"state": { "type": "string", "example": "CA" },
"zip": { "type": "string", "example": "94301" }
},
"xml": { "name": "address" },
"x-readme-ref-name": "Address"
}
],
"required": ["streetName", "streetNumber"],
"x-readme-ref-name": "FullAddress"
}
6 changes: 5 additions & 1 deletion examples/typescript/src/gen/ts/models/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export type Address = {
/**
* @type string | undefined
*/
street?: string
streetName?: string
/**
* @type string | undefined
*/
streetNumber?: string
/**
* @type string | undefined
*/
Expand Down
13 changes: 13 additions & 0 deletions examples/typescript/src/gen/ts/models/FullAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Address } from './Address.ts'

export type FullAddress = Address & {
/**
* @type string
*/
streetNumber: string
} & {
/**
* @type string
*/
streetName: string
}
1 change: 1 addition & 0 deletions examples/typescript/src/gen/ts/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type {
FindPetsByStatusQuery,
} from './FindPetsByStatus.ts'
export type { FindPetsByTagsQueryParams, FindPetsByTags200, FindPetsByTags400, FindPetsByTagsQueryResponse, FindPetsByTagsQuery } from './FindPetsByTags.ts'
export type { FullAddress } from './FullAddress.ts'
export type { GetInventory200, GetInventoryQueryResponse, GetInventoryQuery } from './GetInventory.ts'
export type { GetOrderByIdPathParams, GetOrderById200, GetOrderById400, GetOrderById404, GetOrderByIdQueryResponse, GetOrderByIdQuery } from './GetOrderById.ts'
export type { GetPetByIdPathParams, GetPetById200, GetPetById400, GetPetById404, GetPetByIdQueryResponse, GetPetByIdQuery } from './GetPetById.ts'
Expand Down
19 changes: 17 additions & 2 deletions examples/typescript/src/gen/ts/models/oas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,11 @@ export const oas = {
Address: {
type: 'object',
properties: {
street: {
streetName: {
type: 'string',
},
streetNumber: {
type: 'string',
example: '437 Lytton',
},
city: {
type: 'string',
Expand Down Expand Up @@ -1199,6 +1201,19 @@ export const oas = {
},
required: ['type'],
},
FullAddress: {
properties: {
streetName: {
type: 'string',
},
},
allOf: [
{
$ref: '#/components/schemas/Address',
},
],
required: ['streetName', 'streetNumber'],
},
AddPetRequest: {
required: ['name', 'photoUrls'],
type: 'object',
Expand Down
11 changes: 8 additions & 3 deletions packages/plugin-oas/src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,15 @@ export class SchemaGenerator<
.filter(Boolean)

const items = schemaWithoutAllOf.required
.filter((key) =>
.filter((key) => {
// filter out keys that are already part of the properties(reduce duplicated keys(https://github.com/kubb-labs/kubb/issues/1492)
schemaWithoutAllOf.properties ? !Object.keys(schemaWithoutAllOf.properties).includes(key) : false,
)
if (schemaWithoutAllOf.properties) {
return !Object.keys(schemaWithoutAllOf.properties).includes(key)
}

// schema should include required fields when necessary https://github.com/kubb-labs/kubb/issues/1522
return true
})
.map((key) => {
const schema = schemas.find((item) => item.properties && Object.keys(item.properties).find((propertyKey) => propertyKey === key))

Expand Down
Loading

0 comments on commit f5bae0d

Please sign in to comment.