Skip to content

Commit

Permalink
Merge pull request #1594 from hey-api/fix/text-plain-serializer
Browse files Browse the repository at this point in the history
fix: generate correct response for text/plain content type
  • Loading branch information
mrlubos authored Jan 21, 2025
2 parents c26e792 + bc66cde commit 548e5d8
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-dancers-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: generate correct response for text/plain content type
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/ir/__tests__/mediaType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('isMediaTypeFileLike', () => {
mediaType: 'text/javascript; charset=ISO-8859-1',
},
{
fileLike: true,
fileLike: false,
mediaType: 'text/plain; charset=utf-8',
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/ir/mediaType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fileLikeRegExp =
/^(application\/(pdf|rtf|msword|vnd\.(ms-|openxmlformats-officedocument\.)|zip|x-(7z|tar|rar|zip|iso)|octet-stream|gzip|x-msdownload|json\+download|xml|x-yaml|x-7z-compressed|x-tar)|text\/(plain|yaml|css|javascript)|audio\/(mpeg|wav)|video\/(mp4|x-matroska)|image\/(vnd\.adobe\.photoshop|svg\+xml))(; ?charset=[^;]+)?$/i;
/^(application\/(pdf|rtf|msword|vnd\.(ms-|openxmlformats-officedocument\.)|zip|x-(7z|tar|rar|zip|iso)|octet-stream|gzip|x-msdownload|json\+download|xml|x-yaml|x-7z-compressed|x-tar)|text\/(yaml|css|javascript)|audio\/(mpeg|wav)|video\/(mp4|x-matroska)|image\/(vnd\.adobe\.photoshop|svg\+xml))(; ?charset=[^;]+)?$/i;
const jsonMimeRegExp = /^application\/(.*\+)?json(;.*)?$/i;
const multipartFormDataMimeRegExp = /^multipart\/form-data(;.*)?$/i;
const xWwwFormUrlEncodedMimeRegExp =
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/test/2.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ describe(`OpenAPI ${version}`, () => {
});

const scenarios = [
{
config: createConfig({
input: 'body-response-text-plain.yaml',
output: 'body-response-text-plain',
}),
description: 'handle text/plain content type',
},
{
config: createConfig({
input: 'enum-names-values.json',
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/test/3.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ describe(`OpenAPI ${version}`, () => {
description:
'generates correct array when items are oneOf array with single item',
},
{
config: createConfig({
input: 'body-response-text-plain.yaml',
output: 'body-response-text-plain',
}),
description: 'handle text/plain content type',
},
{
config: createConfig({
input: 'case.json',
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ describe(`OpenAPI ${version}`, () => {
description:
'generates correct array when items are oneOf array with single item',
},
{
config: createConfig({
input: 'body-response-text-plain.yaml',
output: 'body-response-text-plain',
}),
description: 'handle text/plain content type',
},
{
config: createConfig({
input: 'case.json',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostFooData = {
body: string;
path?: never;
query?: never;
url: '/foo';
};

export type PostFooResponses = {
/**
* OK
*/
200: string;
};

export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostFooData = {
body: string;
path?: never;
query?: never;
url: '/foo';
};

export type PostFooResponses = {
/**
* OK
*/
200: string;
};

export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostFooData = {
body: string;
path?: never;
query?: never;
url: '/foo';
};

export type PostFooResponses = {
/**
* OK
*/
200: string;
};

export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
6 changes: 3 additions & 3 deletions packages/openapi-ts/test/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { defineConfig } from '../src';
export default defineConfig({
client: {
// bundle: true,
name: '@hey-api/client-nuxt',
name: '@hey-api/client-fetch',
// name: 'legacy/xhr',
},
// experimentalParser: false,
input: {
// exclude: '^#/components/schemas/ModelWithCircularReference$',
// include:
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
path: './packages/openapi-ts/test/spec/3.1.x/full.json',
path: './packages/openapi-ts/test/spec/2.0.x/body-response-text-plain.yaml',
// path: './test/spec/v3-transforms.json',
// path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
// path: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml',
Expand Down Expand Up @@ -74,7 +74,7 @@ export default defineConfig({
},
// @ts-ignore
{
name: 'zod',
// name: 'zod',
},
],
// useOptions: false,
Expand Down
22 changes: 22 additions & 0 deletions packages/openapi-ts/test/spec/2.0.x/body-response-text-plain.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
swagger: '2.0'
info:
title: OpenAPI 2.0 body response text plain example
version: '1'
paths:
/foo:
post:
consumes:
- text/plain
produces:
- text/plain
parameters:
- name: body
in: body
required: true
schema:
type: string
responses:
'200':
description: OK
schema:
type: string
20 changes: 20 additions & 0 deletions packages/openapi-ts/test/spec/3.0.x/body-response-text-plain.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
openapi: 3.0.4
info:
title: OpenAPI 3.0.4 body response text plain example
version: 1
paths:
/foo:
post:
requestBody:
content:
'text/plain':
schema:
type: string
required: true
responses:
'200':
description: OK
content:
text/plain:
schema:
type: string
20 changes: 20 additions & 0 deletions packages/openapi-ts/test/spec/3.1.x/body-response-text-plain.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
openapi: 3.1.1
info:
title: OpenAPI 3.1.1 body response text plain example
version: 1
paths:
/foo:
post:
requestBody:
content:
'text/plain':
schema:
type: string
required: true
responses:
'200':
description: OK
content:
text/plain:
schema:
type: string

0 comments on commit 548e5d8

Please sign in to comment.