Skip to content

Commit

Permalink
fix(getters): handle OpenAPI 3.1 nullability (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
geirsagberg authored Nov 7, 2022
1 parent 2d7bbb9 commit 622919a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/core/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export const getObject = ({
});
}

if (item.type instanceof Array) {
return combineSchemas({
schema: { anyOf: item.type.map((type) => ({ type })) },
name,
separator: 'anyOf',
context,
nullable,
});
}

if (item.properties && Object.entries(item.properties).length > 0) {
return Object.entries(item.properties).reduce(
(
Expand Down
10 changes: 10 additions & 0 deletions src/core/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ export const getScalar = ({
};
}

case 'null':
return {
value: 'null',
isEnum: false,
type: 'null',
imports: [],
schemas: [],
isRef: false,
};

case 'object':
default: {
const { value, ...rest } = getObject({
Expand Down
7 changes: 7 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ export default defineConfig({
mode: 'tags-split',
},
},
'null-type': {
input: '../specifications/null-type.yaml',
output: {
schemas: '../generated/default/null-type/model',
target: '../generated/default/null-type/endpoints.ts',
},
},
});
56 changes: 56 additions & 0 deletions tests/specifications/null-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
openapi: 3.1.0
info:
title: Nullables
description: 'Nullable types'
version: 1.0.0
tags:
- name: nullables
description: Nullable types
servers:
- url: http://localhost
paths:
/nullable:
get:
tags:
- nullables
summary: Nullable response
operationId: fetchNullable
responses:
200:
description: Successful Operation
content:
application/json:
schema:
type:
- 'string'
- 'null'
/nullable-object:
get:
tags:
- nullables
summary: Nullable object response
operationId: fetchNullableObject
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/NullableObject'
components:
schemas:
NullableObject:
type: object
properties:
name:
type:
- 'string'
- 'null'
age:
anyOf:
- type: 'integer'
- type: 'null'
address:
oneOf:
- type: 'string'
- type: 'null'
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4794,11 +4794,11 @@ [email protected]:
is-wsl "^2.2.0"

openapi3-ts@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/openapi3-ts/-/openapi3-ts-3.0.0.tgz#6c13236c6071bf147c2abf2210f7b11dd107c634"
integrity sha512-LqX4+nSPXreJcgV/QdcazH2Mg5V9yHznTV08WNWCqjNO7d0QiPVlkq0Tp5bWVglsmZkjgVgr5/cFDRY6MjHrUg==
version "3.1.1"
resolved "https://registry.yarnpkg.com/openapi3-ts/-/openapi3-ts-3.1.1.tgz#6e5ffa44a9b47098c8b0a1c58794875bd7a6d967"
integrity sha512-lNUS87ncFqm1m8W21sbTGIeD7sydkQ4nnRUrkBiGlAzTngr50eBKU2suUXhdEk51z6m4cCgaXdB1SFgXtyn81Q==
dependencies:
yaml "^2.1.1"
yaml "^2.1.3"

optionator@^0.8.1:
version "0.8.3"
Expand Down Expand Up @@ -6635,10 +6635,10 @@ yaml@^1.10.0, yaml@^1.10.2:
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==

yaml@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.1.1.tgz#1e06fb4ca46e60d9da07e4f786ea370ed3c3cfec"
integrity sha512-o96x3OPo8GjWeSLF+wOAbrPfhFOGY0W00GNaxCDv+9hkcDJEnev1yh8S7pgHF0ik6zc8sQLuL8hjHjJULZp8bw==
yaml@^2.1.1, yaml@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.1.3.tgz#9b3a4c8aff9821b696275c79a8bee8399d945207"
integrity sha512-AacA8nRULjKMX2DvWvOAdBZMOfQlypSFkjcOcu9FalllIDJ1kvlREzcdIZmidQUqqeMv7jorHjq2HlLv/+c2lg==

[email protected], yargs-parser@^21.0.0:
version "21.1.1"
Expand Down

1 comment on commit 622919a

@vercel
Copy link

@vercel vercel bot commented on 622919a Nov 7, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.