Skip to content

Commit

Permalink
feat(reader): 支持 Blob 类型解读
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Apr 15, 2023
1 parent 5ccf61e commit 9d00a15
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/readers/BaseReader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenAPIV3 } from 'openapi-types';
import { INTERNAL_TYPE_NAMES } from '../const';
import { INTERNAL_TYPE_NAMES, JSON_MIME } from '../const';
import { Named } from './Named';
import { ReaderOptions, StrictReaderOptions, TypeAlias, TypeItem } from './types';

Expand All @@ -8,7 +8,7 @@ export class BaseReader {

static defaults: StrictReaderOptions = {
okCode: 200,
okMediaType: 'application/json',
okMediaType: JSON_MIME,
requestPathTypeName: 'ReqPath',
requestQueryTypeName: 'ReqParams',
requestBodyTypeName: 'ReqData',
Expand Down
24 changes: 18 additions & 6 deletions src/readers/PathsReader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { OpenAPIV3 } from 'openapi-types';
import { BLOB_MIME, JSON_MIME } from '../const';
import { ComponentsReader } from './ComponentsReader';
import { methods } from './const';
import { TypeList, TypeOperation, TypeOperations, TypeOrigin } from './types';
import { TypeItem, TypeList, TypeOperation, TypeOperations, TypeOrigin } from './types';

export class PathsReader extends ComponentsReader {
readingUrl = '';
Expand Down Expand Up @@ -117,15 +118,26 @@ export class PathsReader extends ComponentsReader {
});
}

readOperationRequest(name: string, body: OpenAPIV3.OperationObject['requestBody']) {
readOperationRequest(name: string, body: OpenAPIV3.OperationObject['requestBody']): TypeItem | undefined {
if (!body) return;
if (this.isReference(body)) return;

const { content } = body;
const okMedia = content[this.options.okMediaType];
if (!okMedia) return;

return this.readOperationMedia(name, okMedia);
const jsonReq = content[JSON_MIME];
const blobReq = content[BLOB_MIME];

if (jsonReq) return this.readOperationMedia(name, jsonReq);
if (blobReq)
return {
kind: 'alias',
name,
root: false,
target: 'Blob',
origin: 'Blob',
props: [],
required: true,
ref: '',
};
}

protected readOperationResponse(name: string, responses: NonNullable<OpenAPIV3.ResponsesObject>) {
Expand Down
2 changes: 1 addition & 1 deletion src/readers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface ReaderOptions {
okCode?: number;

/**
* ok 的媒体类型
* ok 的响应类型
* @default ["application/json"]
*/
okMediaType?: string;
Expand Down
11 changes: 10 additions & 1 deletion test/readers/DocumentReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,16 @@ test('DocumentReader', () => {
"method": "post",
"name": "uploadFile",
"request": {
"body": undefined,
"body": {
"kind": "alias",
"name": "UploadFileReqData",
"origin": "Blob",
"props": [],
"ref": "",
"required": true,
"root": false,
"target": "Blob",
},
"path": {
"children": [
{
Expand Down
50 changes: 50 additions & 0 deletions test/readers/PathsReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,56 @@ test('req body', () => {
]);
});

test('req file', () => {
const reader = new PathsReader({
info: {
title: 'test',
version: '1.0.0',
},
openapi: '3.0.0',
paths: {
'/pet': {
get: {
operationId: 'findPet',
requestBody: {
content: {
'application/octet-stream': {
schema: {
type: 'string',
format: 'binary',
},
},
},
},
responses: {},
},
},
},
});

const t = reader.readPaths();
expect(t).toEqual<TypeOperations>([
{
name: 'findPet',
method: HttpMethods.GET,
url: '/pet',
request: {
body: {
kind: 'alias',
name: 'FindPetReqData',
origin: 'Blob',
props: [],
ref: '',
required: true,
root: false,
target: 'Blob',
},
},
response: {},
},
]);
});

test('req query + path', () => {
const reader = new PathsReader({
info: {
Expand Down

0 comments on commit 9d00a15

Please sign in to comment.