Skip to content

Commit 8305dbb

Browse files
author
Artem Parkhomenko
committed
change approach for 3.0 after review
1 parent 246b15c commit 8305dbb

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ export const serializeOperationObject = combineReader(
257257
fold(
258258
() => 'json',
259259
produces => {
260-
console.log(produces);
261260
if (produces.includes('application/octet-stream')) {
262261
return 'blob';
263262
}

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
getTypeName,
66
getURL,
77
HTTPMethod,
8-
JSON_RESPONSE_TYPE,
98
XHRResponseType,
109
} from '../../common/utils';
1110
import {
@@ -43,7 +42,7 @@ import { ResolveRefContext, fromString, getRelativePath, Ref } from '../../../..
4342
import { OperationObject } from '../../../../schema/3.0/operation-object';
4443
import { ParameterObject, ParameterObjectCodec } from '../../../../schema/3.0/parameter-object';
4544
import { RequestBodyObjectCodec } from '../../../../schema/3.0/request-body-object';
46-
import { chain, exists, getOrElse, isSome, none, Option, some, map } from 'fp-ts/lib/Option';
45+
import { chain, isSome, none, Option, some, map, fromEither, fold } from 'fp-ts/lib/Option';
4746
import { constFalse } from 'fp-ts/lib/function';
4847
import { clientRef } from '../../common/bundled/client';
4948
import { Kind } from '../../../../utils/types';
@@ -57,8 +56,8 @@ import {
5756
serializedFragment,
5857
SerializedFragment,
5958
} from '../../common/data/serialized-fragment';
60-
import { PrimitiveSchemaObjectCodec, SchemaObjectCodec } from '../../../../schema/3.0/schema-object';
61-
import { lookup } from 'fp-ts/lib/Record';
59+
import { SchemaObjectCodec } from '../../../../schema/3.0/schema-object';
60+
import { lookup, keys } from 'fp-ts/lib/Record';
6261
import { ResponseObjectCodec } from '../../../../schema/3.0/response-object';
6362

6463
const getOperationName = (pattern: string, operation: OperationObject, method: HTTPMethod): string =>
@@ -261,28 +260,25 @@ export const serializeOperationObject = combineReader(
261260

262261
const responseType: XHRResponseType = pipe(
263262
lookup('200', operation.responses),
264-
chain(response => e.deepLookup(response, ResponseObjectCodec, ReferenceObjectCodec)),
263+
chain(response =>
264+
ReferenceObjectCodec.is(response)
265+
? fromEither(e.resolveRef(response.$ref, ResponseObjectCodec))
266+
: some(response),
267+
),
265268
chain(response => response.content),
266-
chain(content => lookup('application/json', content)),
267-
chain(contentBody => contentBody.schema),
268-
chain(schema => e.deepLookup(schema, PrimitiveSchemaObjectCodec, ReferenceObjectCodec)),
269-
map(schema => {
270-
const isBinary = pipe(
271-
schema.format,
272-
exists(format => format === 'binary'),
273-
);
274-
275-
if (schema.type === 'string' && isBinary) {
276-
return 'blob';
277-
}
278-
279-
if (schema.type === 'string') {
280-
return 'text';
281-
}
282-
283-
return 'json';
284-
}),
285-
getOrElse(() => JSON_RESPONSE_TYPE),
269+
map(keys),
270+
fold(
271+
() => 'json',
272+
types => {
273+
if (types.includes('application/octet-stream')) {
274+
return 'blob';
275+
}
276+
if (types.includes('text/plain')) {
277+
return 'text';
278+
}
279+
return 'json';
280+
},
281+
),
286282
);
287283

288284
return combineEither(

test/specs/3.0/file-and-text.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ paths:
2424
200:
2525
description: succesfull operation
2626
content:
27-
application/json:
27+
application/octet-stream:
2828
schema:
2929
type: string
3030
format: binary
@@ -49,7 +49,7 @@ paths:
4949
200:
5050
description: succesfull operation
5151
content:
52-
application/json:
52+
application/octet-stream:
5353
schema:
5454
$ref: '#/components/schemas/File'
5555
/text/1/version/1:
@@ -62,7 +62,7 @@ paths:
6262
200:
6363
description: succesfull operation
6464
content:
65-
application/json:
65+
text/plain:
6666
schema:
6767
type: string
6868
/textWithResponseRef:
@@ -75,7 +75,7 @@ paths:
7575
200:
7676
description: ok
7777
content:
78-
application/json:
78+
text/plain:
7979
schema:
8080
$ref: '#/components/responses/SuccessfulText'
8181
/textWithSchemaRef:
@@ -88,7 +88,7 @@ paths:
8888
200:
8989
description: succesfull operation
9090
content:
91-
application/json:
91+
text/plain:
9292
schema:
9393
$ref: '#/components/schemas/Text'
9494
components:
@@ -104,12 +104,12 @@ components:
104104
SuccessfulFile:
105105
description: succesfull operation
106106
content:
107-
application/json:
107+
application/octet-stream:
108108
schema:
109109
$ref: '#/components/schemas/File'
110110
SuccessfulText:
111111
description: succesful text data loading
112112
content:
113-
application/json:
113+
text/plain:
114114
schema:
115115
$ref: '#/components/schemas/Text'

0 commit comments

Comments
 (0)