Skip to content

Commit 246b15c

Browse files
author
Artem Parkhomenko
committed
change approach for 2.0 after review
1 parent 81177a6 commit 246b15c

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/language/typescript/2.0/serializers/operation-object.ts

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@ import {
99
SerializedType,
1010
} from '../../common/data/serialized-type';
1111
import { pipe } from 'fp-ts/lib/pipeable';
12-
import { getOrElse, isSome, map, chain, Option, exists } from 'fp-ts/lib/Option';
12+
import { getOrElse, isSome, map, Option, fold } from 'fp-ts/lib/Option';
1313
import { serializeOperationResponses } from './responses-object';
1414
import { fromSerializedType } from '../../common/data/serialized-parameter';
1515
import { getSerializedKindDependency, serializedDependency } from '../../common/data/serialized-dependency';
1616
import { concatIf } from '../../../../utils/array';
1717
import { when } from '../../../../utils/string';
18-
import {
19-
getJSDoc,
20-
getKindValue,
21-
getSafePropertyName,
22-
getURL,
23-
HTTPMethod,
24-
JSON_RESPONSE_TYPE,
25-
XHRResponseType,
26-
} from '../../common/utils';
18+
import { getJSDoc, getKindValue, getSafePropertyName, getURL, HTTPMethod, XHRResponseType } from '../../common/utils';
2719
import { Either, isLeft, left, right } from 'fp-ts/lib/Either';
2820
import { array, either, nonEmptyArray, option, record } from 'fp-ts';
2921
import { combineEither } from '@devexperts/utils/dist/adt/either.utils';
@@ -61,9 +53,6 @@ import {
6153
} from '../../common/data/serialized-fragment';
6254
import { sequenceOptionEither } from '../../../../utils/option';
6355
import { identity } from 'fp-ts/lib/function';
64-
import { lookup } from 'fp-ts/lib/Record';
65-
import { PrimitiveSchemaObjectCodec } from '../../../../schema/2.0/schema-object';
66-
import { ResponseObject } from '../../../../schema/2.0/response-object';
6756

6857
interface Parameters {
6958
readonly pathParameters: PathParameterObject[];
@@ -264,29 +253,24 @@ export const serializeOperationObject = combineReader(
264253
);
265254

266255
const responseType: XHRResponseType = pipe(
267-
lookup('200', operation.responses),
268-
chain(response => e.deepLookup(response, ResponseObject, ReferenceObjectCodec)),
269-
chain(response => response.schema),
270-
chain(schema => e.deepLookup(schema, PrimitiveSchemaObjectCodec, ReferenceObjectCodec)),
271-
map(schema => {
272-
const isBinary = pipe(
273-
schema.format,
274-
exists(format => format === 'binary'),
275-
);
276-
277-
if (schema.type === 'string' && isBinary) {
278-
return 'blob';
279-
}
280-
281-
if (schema.type === 'string') {
282-
return 'text';
283-
}
284-
285-
return 'json';
286-
}),
287-
getOrElse(() => JSON_RESPONSE_TYPE),
256+
operation.produces,
257+
fold(
258+
() => 'json',
259+
produces => {
260+
console.log(produces);
261+
if (produces.includes('application/octet-stream')) {
262+
return 'blob';
263+
}
264+
if (produces.includes('text/plain')) {
265+
return 'text';
266+
}
267+
return 'json';
268+
},
269+
),
288270
);
289271

272+
console.log(responseType, operation.produces);
273+
290274
return combineEither(
291275
parameters,
292276
serializedResponses,

test/specs/2.0/yaml/demo.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ paths:
8686
- files
8787
summary: GetSomeFile
8888
operationId: getFile
89+
produces:
90+
- application/octet-stream
8991
responses:
9092
200:
9193
description: succesfull operation
@@ -98,6 +100,8 @@ paths:
98100
- files
99101
summary: GetFileWithResponseRef
100102
operationId: getFileWithResponseRef
103+
produces:
104+
- application/octet-stream
101105
responses:
102106
200:
103107
$ref: '#/responses/SuccessfulFile'
@@ -107,6 +111,8 @@ paths:
107111
- files
108112
summary: GetFileWithSchemaRef
109113
operationId: getFileWithSchemaRef
114+
produces:
115+
- application/octet-stream
110116
responses:
111117
200:
112118
description: succesfull operation
@@ -127,6 +133,8 @@ paths:
127133
- text
128134
summary: GetSomeText
129135
operationId: getText
136+
produces:
137+
- text/plain
130138
responses:
131139
200:
132140
description: succesfull operation
@@ -138,6 +146,8 @@ paths:
138146
- text
139147
summary: GetTextWithResponseRef
140148
operationId: getTextWithResponseRef
149+
produces:
150+
- text/plain
141151
responses:
142152
200:
143153
$ref: '#/responses/SuccessfulText'
@@ -147,6 +157,8 @@ paths:
147157
- text
148158
summary: GetTextWithSchemaRef
149159
operationId: getTextWithSchemaRef
160+
produces:
161+
- text/plain
150162
responses:
151163
200:
152164
description: succesfull operation

0 commit comments

Comments
 (0)