Skip to content

Commit

Permalink
fix: remove duplicated keys when set in required
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Jan 8, 2025
1 parent 2f11f13 commit 5febbe5
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-wombats-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-oas": patch
---

remove duplicated keys when set in required
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Changelog

# Changelog

## 3.4.2
- [`plugin-oas`](/plugins/plugin-oas): remove duplicated keys when set in required

## 3.4.1
- [`plugin-faker`](/plugins/plugin-faker): min and max was not applied to the faker functions

Expand Down
349 changes: 349 additions & 0 deletions examples/advanced/configs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
{
"openapi": "3.0.0",
"info": {
"title": "X",
"version": "2025-01-01"
},
"tags": [],
"paths": {},
"components": {
"schemas": {
"BaseDateFilter": {
"type": "object",
"required": ["type", "operand"],
"properties": {
"type": {
"type": "string",
"enum": ["date"]
},
"operand": {
"$ref": "#/components/schemas/DateFilterOperand"
}
},
"discriminator": {
"propertyName": "operand",
"mapping": {
"between": "#/components/schemas/DateBetweenFilter",
"hasValue": "#/components/schemas/DateHasValueFilter",
"hasNoValue": "#/components/schemas/DateHasNoValueFilter"
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseFilter"
}
]
},
"BaseDateFilterWithTimeZone": {
"type": "object",
"properties": {
"timezone": {
"type": "string"
},
"timezoneVariant": {
"type": "string",
"enum": ["utc", "organization", "local"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseDateFilter"
}
]
},
"BaseFilter": {
"type": "object",
"required": ["id", "type"],
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "type",
"mapping": {
"multiSelect": "#/components/schemas/MultiSelectFilterValue",
"text": "#/components/schemas/TextFilterValue",
"metadata": "#/components/schemas/MetadataFilterValue"
}
}
},
"DateBetweenFilter": {
"type": "object",
"required": ["operand", "from", "to"],
"properties": {
"operand": {
"type": "string",
"enum": ["between"]
},
"from": {
"type": "string",
"format": "date-time"
},
"to": {
"type": "string",
"format": "date-time"
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseDateFilterWithTimeZone"
}
]
},
"DateEqualsFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["equals"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"DateFilterOperand": {
"type": "string",
"enum": ["equals", "notEqualTo", "lessThan", "greaterThan", "lessOrEqual", "greaterOrEqual", "between", "hasValue", "hasNoValue"]
},
"DateFilterValue": {
"anyOf": [
{
"$ref": "#/components/schemas/DateEqualsFilter"
},
{
"$ref": "#/components/schemas/DateNotEqualsFilter"
},
{
"$ref": "#/components/schemas/DateLessThanFilter"
},
{
"$ref": "#/components/schemas/DateGreaterThanFilter"
},
{
"$ref": "#/components/schemas/DateLessOrEqualFilter"
},
{
"$ref": "#/components/schemas/DateGreaterOrEqualFilter"
},
{
"$ref": "#/components/schemas/DateBetweenFilter"
},
{
"$ref": "#/components/schemas/DateHasValueFilter"
},
{
"$ref": "#/components/schemas/DateHasNoValueFilter"
}
]
},
"DateGreaterOrEqualFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["greaterOrEqual"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"DateGreaterThanFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["greaterThan"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"DateHasNoValueFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["hasNoValue"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseDateFilter"
}
]
},
"DateHasValueFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["hasValue"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseDateFilter"
}
]
},
"DateLessOrEqualFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["lessOrEqual"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"DateLessThanFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["lessThan"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"DateNotEqualsFilter": {
"type": "object",
"required": ["operand"],
"properties": {
"operand": {
"type": "string",
"enum": ["notEqualTo"]
}
},
"allOf": [
{
"$ref": "#/components/schemas/SingleDateFilter"
}
]
},
"MetadataFilterValue": {
"type": "object",
"required": ["type", "operand", "key"],
"properties": {
"type": {
"type": "string",
"enum": ["metadata"]
},
"operand": {
"type": "string",
"enum": ["equals"]
},
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseFilter"
}
]
},
"MultiSelectFilterOperand": {
"type": "string",
"enum": ["is", "isNot"]
},
"MultiSelectFilterValue": {
"type": "object",
"required": ["type", "operand", "value"],
"properties": {
"type": {
"type": "string",
"enum": ["multiSelect"]
},
"operand": {
"$ref": "#/components/schemas/MultiSelectFilterOperand"
},
"value": {
"type": "array",
"items": {}
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseFilter"
}
]
},
"SingleDateFilter": {
"type": "object",
"required": ["operand", "value"],
"properties": {
"operand": {
"type": "string",
"enum": ["equals", "notEqualTo", "lessThan", "greaterThan", "lessOrEqual", "greaterOrEqual"]
},
"value": {
"type": "string",
"format": "date-time"
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseDateFilterWithTimeZone"
}
]
},
"TextFilterOperand": {
"type": "string",
"enum": ["is", "isNot", "startsWith", "endsWith", "contains", "doesNotContain", "isEmpty", "isNotEmpty"]
},
"TextFilterValue": {
"type": "object",
"required": ["type", "operand", "value"],
"properties": {
"type": {
"type": "string",
"enum": ["text"]
},
"operand": {
"$ref": "#/components/schemas/TextFilterOperand"
},
"value": {
"type": "string"
}
},
"allOf": [
{
"$ref": "#/components/schemas/BaseFilter"
}
]
}
}
}
}
5 changes: 5 additions & 0 deletions packages/plugin-oas/src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ export class SchemaGenerator<
}

if (schemaWithoutAllOf.required) {
// TODO use of Required ts helper instead
const schemas = schema.allOf
.map((item) => {
if (isReference(item)) {
Expand All @@ -563,6 +564,10 @@ export class SchemaGenerator<
.filter(Boolean)

const items = schemaWithoutAllOf.required
.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,
)
.map((key) => {
const schema = schemas.find((item) => item.properties && Object.keys(item.properties).find((propertyKey) => propertyKey === key))

Expand Down
Loading

0 comments on commit 5febbe5

Please sign in to comment.