Skip to content

Commit

Permalink
fix: Use of x-nullable and nullable for additionalProperties.
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Sep 22, 2024
1 parent e5c0424 commit ba02488
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fair-mice-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-oas": patch
---

Use of `x-nullable` and `nullable` for additionalProperties.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ title: Changelog

# Changelog

## 3.0.0-alpha.22
- [`plugin-zod`](/plugins/plugin-zod): Use of `x-nullable` and `nullable` for additionalProperties.


## 3.0.0-alpha.21
- [`plugin-faker`](/plugins/plugin-faker): use of `faker.image.url()` instead of `faker.image.imageUrl()`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ exports[`faker parse > 'min' 1`] = `undefined`;
exports[`faker parse > 'nullable' 1`] = `undefined`;
exports[`faker parse > 'nullableAdditionalProperties' 1`] = `"{}"`;
exports[`faker parse > 'number' 1`] = `"faker.number.float()"`;
exports[`faker parse > 'object' 1`] = `"{"firstName": faker.string.alpha(),"address": faker.string.alpha()}"`;
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-oas/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ components:
type: string
tag:
type: string
schema:
type: object
additionalProperties:
x-nullable: true
type: string
Pets:
type: array
items:
Expand Down
50 changes: 50 additions & 0 deletions packages/plugin-oas/mocks/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ const basic: Array<{ name: string; schema: Schema }> = [
keyword: schemaKeywords.blob,
},
},
{
name: 'nullableAdditionalProperties',
schema: {
keyword: schemaKeywords.object,
args: {
properties: {},
additionalProperties: [
{
keyword: schemaKeywords.string,
},
{
args: {
format: undefined,
type: schemaKeywords.string,
},
keyword: schemaKeywords.schema,
},
{
keyword: schemaKeywords.nullable,
},
],
},
},
},
]

const full: Array<{ name: string; schema: Schema[] }> = [
Expand Down Expand Up @@ -556,6 +580,32 @@ const full: Array<{ name: string; schema: Schema[] }> = [
},
],
},
{
name: 'nullableAdditionalProperties',
schema: [
{
keyword: schemaKeywords.object,
args: {
properties: {},
additionalProperties: [
{
keyword: schemaKeywords.string,
},
{
args: {
format: undefined,
type: schemaKeywords.string,
},
keyword: schemaKeywords.schema,
},
{
keyword: schemaKeywords.number,
},
],
},
},
],
},
{
name: 'Record',
schema: [
Expand Down
37 changes: 37 additions & 0 deletions packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,43 @@ exports[`SchemaGenerator core > 'Pet' 1`] = `
"keyword": "name",
},
],
"schema": [
{
"args": {
"additionalProperties": [
{
"keyword": "string",
},
{
"args": {
"format": undefined,
"type": "string",
},
"keyword": "schema",
},
{
"keyword": "nullable",
},
],
"properties": {},
},
"keyword": "object",
},
{
"args": {
"format": undefined,
"type": "object",
},
"keyword": "schema",
},
{
"args": "schema",
"keyword": "name",
},
{
"keyword": "optional",
},
],
"tag": [
{
"keyword": "string",
Expand Down
93 changes: 93 additions & 0 deletions packages/plugin-ts/src/parser/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,99 @@ exports[`type parse > 'min' 1`] = `undefined`;

exports[`type parse > 'nullable' 1`] = `undefined`;

exports[`type parse > 'nullableAdditionalProperties' 1`] = `
NodeObject {
"emitNode": undefined,
"end": -1,
"flags": 16,
"id": 0,
"kind": 187,
"localSymbol": undefined,
"members": [
NodeObject {
"emitNode": undefined,
"end": -1,
"flags": 16,
"id": 0,
"jsDoc": undefined,
"kind": 181,
"localSymbol": undefined,
"locals": undefined,
"modifierFlagsCache": 0,
"modifiers": undefined,
"nextContainer": undefined,
"original": undefined,
"parameters": [
NodeObject {
"dotDotDotToken": undefined,
"emitNode": undefined,
"end": -1,
"flags": 16,
"id": 0,
"initializer": undefined,
"jsDoc": undefined,
"kind": 169,
"localSymbol": undefined,
"modifierFlagsCache": 0,
"modifiers": undefined,
"name": IdentifierObject {
"emitNode": undefined,
"end": -1,
"escapedText": "key",
"flags": 16,
"flowNode": undefined,
"id": 0,
"jsDoc": undefined,
"kind": 80,
"parent": undefined,
"pos": -1,
"symbol": undefined,
"transformFlags": 0,
},
"original": undefined,
"parent": undefined,
"pos": -1,
"questionToken": undefined,
"symbol": undefined,
"transformFlags": 1,
"type": TokenObject {
"emitNode": undefined,
"end": -1,
"flags": 16,
"id": 0,
"kind": 154,
"parent": undefined,
"pos": -1,
"transformFlags": 1,
},
},
],
"parent": undefined,
"pos": -1,
"symbol": undefined,
"transformFlags": 1,
"type": TokenObject {
"emitNode": undefined,
"end": -1,
"flags": 16,
"id": 0,
"kind": 154,
"parent": undefined,
"pos": -1,
"transformFlags": 1,
},
"typeArguments": undefined,
},
],
"modifierFlagsCache": 0,
"original": undefined,
"parent": undefined,
"pos": -1,
"symbol": undefined,
"transformFlags": 1,
}
`;

exports[`type parse > 'number' 1`] = `
TokenObject {
"emitNode": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ exports[`zod parse > 'min' 1`] = `".min(2)"`;

exports[`zod parse > 'nullable' 1`] = `".nullable()"`;

exports[`zod parse > 'nullableAdditionalProperties' 1`] = `"z.object({}).catchall(z.string().nullable())"`;

exports[`zod parse > 'number' 1`] = `"z.number()"`;

exports[`zod parse > 'object' 1`] = `"z.object({"firstName": z.string().min(2),"address": z.string().describe("Your address").nullable()})"`;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-zod/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
? current.args.additionalProperties
.map((schema) => parse(current, schema, options))
.filter(Boolean)
.at(0)
.join('')
: undefined

const text = [
Expand Down

0 comments on commit ba02488

Please sign in to comment.