Skip to content

Commit

Permalink
Use text/plain for non-object responses
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Jan 10, 2021
1 parent c1bf138 commit 2a8e3f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
23 changes: 14 additions & 9 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
isPackageSymbol,
getPropertyType,
isNullType,
isStringType,
isNumberType,
isBooleanType,
isBooleanLiteralType,
isNumberLiteralType,
isStringLiteralType,
Expand Down Expand Up @@ -338,12 +341,14 @@ const getResponseDefinition = (
description: status, // TODO: What should the response description be?
...(bodySchema
? {
content: {
// TODO: application/json should probably not be hard-coded
'application/json': {
schema: bodySchema,
},
},
content:
isStringType(bodyType) || isNumberType(bodyType)
? { 'text/plain': { schema: bodySchema } }
: {
'application/json': {
schema: bodySchema,
},
},
}
: undefined),
...(headers
Expand Down Expand Up @@ -467,13 +472,13 @@ const typeToSchema = (
}
}

if (type.flags & ts.TypeFlags.String) {
if (isStringType(type)) {
return { type: 'string', ...nullable }
}
if (type.flags & ts.TypeFlags.Number) {
if (isNumberType(type)) {
return { type: 'number', ...nullable }
}
if (type.flags & ts.TypeFlags.Boolean) {
if (isBooleanType(type)) {
return { type: 'boolean', ...nullable }
}
if (isStringLiteralType(type)) {
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export const isOptional = (symbol: ts.Symbol): boolean =>
!!(symbol.flags & ts.SymbolFlags.Optional)
export const isObjectType = (type: ts.Type): boolean =>
!!(type.flags & ts.TypeFlags.Object)
export const isStringType = (type: ts.Type): boolean =>
!!(type.flags & ts.TypeFlags.String)
export const isNumberType = (type: ts.Type): boolean =>
!!(type.flags & ts.TypeFlags.Number)
export const isBooleanType = (type: ts.Type): boolean =>
!!(type.flags & ts.TypeFlags.Boolean)
export const isBooleanLiteralType = (type: ts.Type): boolean =>
!!(type.flags & ts.TypeFlags.BooleanLiteral)
export const isNumberLiteralType = (
Expand Down
18 changes: 9 additions & 9 deletions tests/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand All @@ -72,7 +72,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand Down Expand Up @@ -119,7 +119,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand Down Expand Up @@ -147,7 +147,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand All @@ -157,7 +157,7 @@ Array [
},
"400": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand Down Expand Up @@ -246,7 +246,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand All @@ -259,7 +259,7 @@ Array [
},
"400": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand All @@ -276,7 +276,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand All @@ -301,7 +301,7 @@ Array [
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"text/plain": Object {
"schema": Object {
"type": "string",
},
Expand Down

0 comments on commit 2a8e3f7

Please sign in to comment.