Skip to content

Commit 0d0f651

Browse files
committed
fix: bugs in new schemas (giphy, microsoft), fix problem with declared params in the root of schema (like in giphy schema)
1 parent 4164699 commit 0d0f651

File tree

6 files changed

+86
-31
lines changed

6 files changed

+86
-31
lines changed

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ module.exports = {
1717

1818
const apiTemplate = fs.readFileSync(path.resolve(__dirname, './templates/api.mustache'), { encoding: 'UTF-8' })
1919

20-
const parsedSchemas = _.map(components && components.schemas, parseSchema)
21-
const routes = parseRoutes(paths, parsedSchemas);
20+
const parsedSchemas = _.map(_.get(components, "schemas"), parseSchema)
21+
const routes = parseRoutes(paths, parsedSchemas, components);
2222
const hasSecurityRoutes = routes.some(route => route.security);
2323
const hasQueryRoutes = routes.some(route => route.hasQuery);
2424
const apiConfig = createApiConfig({ info, servers }, hasSecurityRoutes);

src/routes.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require("lodash");
2-
const { parseSchema } = require("./schema");
2+
const { collect } = require("./utils");
3+
const { parseSchema, getRefType } = require("./schema");
34
const { checkAndRenameModelName } = require("./modelNames");
45
const { inlineExtraFormatters } = require("./typeFormatters");
56

@@ -46,9 +47,10 @@ const getRouteName = (operationId, method, route, moduleName) => {
4647
return createCustomOperationId(method, route, moduleName);
4748
}
4849

49-
const parseRoutes = (routes, parsedSchemas) =>
50+
const parseRoutes = (routes, parsedSchemas, components) =>
5051
_.entries(routes)
5152
.reduce((routes, [route, requestInfoByMethodsMap]) => {
53+
const globalParametersMap = _.get(components, "parameters", {});
5254
parameters = _.get(requestInfoByMethodsMap, 'parameters');
5355

5456
// TODO: refactor that hell
@@ -77,13 +79,25 @@ const parseRoutes = (routes, parsedSchemas) =>
7779
responses,
7880
} = requestInfo;
7981
const hasSecurity = !!(security && security.length);
80-
const pathParams = _.filter(parameters, parameter => parameter.in === 'path');
81-
const queryParams = _.filter(parameters, parameter => parameter.in === 'query');
82+
const pathParams = collect(parameters, parameter => {
83+
if (parameter.in === 'path') return parameter;
84+
85+
const refTypeName = getRefType(parameter);
86+
const globalParam = refTypeName && globalParametersMap[refTypeName]
87+
return globalParam && globalParametersMap[refTypeName].in === "path" && globalParam
88+
})
89+
const queryParams = collect(parameters, parameter => {
90+
if (parameter.in === 'query') return parameter;
91+
92+
const refTypeName = getRefType(parameter);
93+
const globalParam = refTypeName && globalParametersMap[refTypeName]
94+
return globalParam && globalParametersMap[refTypeName].in === "query" && globalParam;
95+
})
8296
const moduleName = _.camelCase(route.split('/').filter(Boolean)[0]);
8397

8498
const routeName = getRouteName(operationId, method, route, moduleName);
85-
86-
const queryObjectSchema = queryParams.length && queryParams.reduce((objectSchema, queryPartSchema) => ({
99+
100+
const queryObjectSchema = _.reduce(queryParams, (objectSchema, queryPartSchema) => ({
87101
...objectSchema,
88102
properties: {
89103
...objectSchema.properties,

src/schema.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ const specificObjectTypes = {
2424
return type === 'primitive' ? `${content}[]` : `Array<${content}>`
2525
}
2626
}
27-
const getRefType = (ref) => _.last(_.split(ref, '/'))
27+
28+
const getRefType = (property) => {
29+
if (!property["$ref"]) return null;
30+
return _.last(_.split(property["$ref"], '/'));
31+
}
32+
2833
const getType = (property) => {
2934
const func = specificObjectTypes[property.type] || (() => getPrimitiveType(property.type))
30-
return property["$ref"] ? getRefType(property["$ref"]) : func(property)
35+
return getRefType(property) || func(property)
3136
}
3237
const getObjectTypeContent = (properties) => {
3338
return _.map(properties, (property, name) => {
@@ -137,5 +142,6 @@ const parseSchema = (schema, typeName, formattersMap) => {
137142
module.exports = {
138143
parseSchema,
139144
getType,
145+
getRefType,
140146
getPrimitiveType,
141147
}

src/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const _ = require("lodash")
2+
3+
// its filter and reduce together :)
4+
const collect = (objectOrArray, cb) =>
5+
_.reduce(objectOrArray, (acc, part, key) => {
6+
const result = cb(part, key);
7+
8+
return _.isArray(objectOrArray) ?
9+
(result ? [ ...acc, result ] : acc) :
10+
(result ? { ...acc, [key]: result } : acc)
11+
}, _.isArray(objectOrArray) ? [] : {})
12+
13+
module.exports = {
14+
collect,
15+
}

tests/generated/v2.0/example1.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ export class Api<SecurityDataType> {
7878
this.securityData = data
7979
}
8080

81+
private addQueryParams(query: object): string {
82+
const keys = Object.keys(query);
83+
return keys.length ? (
84+
'?' +
85+
keys.reduce((paramsArray, param) => [
86+
...paramsArray,
87+
param + '=' + encodeURIComponent(query[param])
88+
], []).join('&')
89+
) : ''
90+
}
8191

8292
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
8393
return {
@@ -126,8 +136,8 @@ export class Api<SecurityDataType> {
126136
* @request POST:/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/managedInstances/{managedInstanceName}/tdeCertificates
127137
* @description Creates a TDE certificate for a given server.
128138
*/
129-
managedInstanceTdeCertificatesCreate: (parameters: TdeCertificate, params?: RequestParams) =>
130-
this.request<any>(`/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Sql/managedInstances/${managedInstanceName}/tdeCertificates`, "POST", params, parameters),
139+
managedInstanceTdeCertificatesCreate: (resourceGroupName: string, managedInstanceName: string, subscriptionId: string, query: { "api-version": string }, parameters: TdeCertificate, params?: RequestParams) =>
140+
this.request<any>(`/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Sql/managedInstances/${managedInstanceName}/tdeCertificates${this.addQueryParams(query)}`, "POST", params, parameters),
131141
}
132142

133143
}

tests/generated/v2.0/giphy.ts

+29-19
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ export class Api<SecurityDataType> {
280280
this.securityData = data
281281
}
282282

283+
private addQueryParams(query: object): string {
284+
const keys = Object.keys(query);
285+
return keys.length ? (
286+
'?' +
287+
keys.reduce((paramsArray, param) => [
288+
...paramsArray,
289+
param + '=' + encodeURIComponent(query[param])
290+
], []).join('&')
291+
) : ''
292+
}
283293

284294
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
285295
return {
@@ -329,8 +339,8 @@ export class Api<SecurityDataType> {
329339
* @request GET:/gifs
330340
* @description A multiget version of the get GIF by ID endpoint..
331341
*/
332-
getGifsById: (params?: RequestParams) =>
333-
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs`, "GET", params, null),
342+
getGifsById: (query: { ids?: string }, params?: RequestParams) =>
343+
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs${this.addQueryParams(query)}`, "GET", params, null),
334344

335345

336346
/**
@@ -340,8 +350,8 @@ export class Api<SecurityDataType> {
340350
* @request GET:/gifs/random
341351
* @description Returns a random GIF, limited by tag. Excluding the tag parameter will return a random GIF from the GIPHY catalog..
342352
*/
343-
randomGif: (params?: RequestParams) =>
344-
this.request<{ data?: Gif, meta?: Meta }>(`/gifs/random`, "GET", params, null),
353+
randomGif: (query: { tag?: string, rating?: string }, params?: RequestParams) =>
354+
this.request<{ data?: Gif, meta?: Meta }>(`/gifs/random${this.addQueryParams(query)}`, "GET", params, null),
345355

346356

347357
/**
@@ -351,8 +361,8 @@ export class Api<SecurityDataType> {
351361
* @request GET:/gifs/search
352362
* @description Search all GIPHY GIFs for a word or phrase. Punctuation will be stripped and ignored. Use a plus or url encode for phrases. Example paul+rudd, ryan+gosling or american+psycho..
353363
*/
354-
searchGifs: (params?: RequestParams) =>
355-
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs/search`, "GET", params, null),
364+
searchGifs: (query: { q: string, limit?: number, offset?: number, rating?: string, lang?: string }, params?: RequestParams) =>
365+
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs/search${this.addQueryParams(query)}`, "GET", params, null),
356366

357367

358368
/**
@@ -362,8 +372,8 @@ export class Api<SecurityDataType> {
362372
* @request GET:/gifs/translate
363373
* @description The translate API draws on search, but uses the GIPHY `special sauce` to handle translating from one vocabulary to another. In this case, words and phrases to GIF.
364374
*/
365-
translateGif: (params?: RequestParams) =>
366-
this.request<{ data?: Gif, meta?: Meta }>(`/gifs/translate`, "GET", params, null),
375+
translateGif: (query: { s: string }, params?: RequestParams) =>
376+
this.request<{ data?: Gif, meta?: Meta }>(`/gifs/translate${this.addQueryParams(query)}`, "GET", params, null),
367377

368378

369379
/**
@@ -373,8 +383,8 @@ export class Api<SecurityDataType> {
373383
* @request GET:/gifs/trending
374384
* @description Fetch GIFs currently trending online. Hand curated by the GIPHY editorial team. The data returned mirrors the GIFs showcased on the GIPHY homepage. Returns 25 results by default..
375385
*/
376-
trendingGifs: (params?: RequestParams) =>
377-
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs/trending`, "GET", params, null),
386+
trendingGifs: (query: { limit?: number, offset?: number, rating?: string }, params?: RequestParams) =>
387+
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/gifs/trending${this.addQueryParams(query)}`, "GET", params, null),
378388

379389

380390
/**
@@ -384,7 +394,7 @@ export class Api<SecurityDataType> {
384394
* @request GET:/gifs/{gifId}
385395
* @description Returns a GIF given that GIF's unique ID.
386396
*/
387-
getGifById: (params?: RequestParams) =>
397+
getGifById: (gifId: number, params?: RequestParams) =>
388398
this.request<{ data?: Gif, meta?: Meta }>(`/gifs/${gifId}`, "GET", params, null),
389399
}
390400
stickers = {
@@ -397,8 +407,8 @@ export class Api<SecurityDataType> {
397407
* @request GET:/stickers/random
398408
* @description Returns a random GIF, limited by tag. Excluding the tag parameter will return a random GIF from the GIPHY catalog..
399409
*/
400-
randomSticker: (params?: RequestParams) =>
401-
this.request<{ data?: Gif, meta?: Meta }>(`/stickers/random`, "GET", params, null),
410+
randomSticker: (query: { tag?: string, rating?: string }, params?: RequestParams) =>
411+
this.request<{ data?: Gif, meta?: Meta }>(`/stickers/random${this.addQueryParams(query)}`, "GET", params, null),
402412

403413

404414
/**
@@ -408,8 +418,8 @@ export class Api<SecurityDataType> {
408418
* @request GET:/stickers/search
409419
* @description Replicates the functionality and requirements of the classic GIPHY search, but returns animated stickers rather than GIFs..
410420
*/
411-
searchStickers: (params?: RequestParams) =>
412-
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/stickers/search`, "GET", params, null),
421+
searchStickers: (query: { q: string, limit?: number, offset?: number, rating?: string, lang?: string }, params?: RequestParams) =>
422+
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/stickers/search${this.addQueryParams(query)}`, "GET", params, null),
413423

414424

415425
/**
@@ -419,8 +429,8 @@ export class Api<SecurityDataType> {
419429
* @request GET:/stickers/translate
420430
* @description The translate API draws on search, but uses the GIPHY `special sauce` to handle translating from one vocabulary to another. In this case, words and phrases to GIFs..
421431
*/
422-
translateSticker: (params?: RequestParams) =>
423-
this.request<{ data?: Gif, meta?: Meta }>(`/stickers/translate`, "GET", params, null),
432+
translateSticker: (query: { s: string }, params?: RequestParams) =>
433+
this.request<{ data?: Gif, meta?: Meta }>(`/stickers/translate${this.addQueryParams(query)}`, "GET", params, null),
424434

425435

426436
/**
@@ -430,8 +440,8 @@ export class Api<SecurityDataType> {
430440
* @request GET:/stickers/trending
431441
* @description Fetch Stickers currently trending online. Hand curated by the GIPHY editorial team. Returns 25 results by default..
432442
*/
433-
trendingStickers: (params?: RequestParams) =>
434-
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/stickers/trending`, "GET", params, null),
443+
trendingStickers: (query: { limit?: number, offset?: number, rating?: string }, params?: RequestParams) =>
444+
this.request<{ data?: Gif[], meta?: Meta, pagination?: Pagination }>(`/stickers/trending${this.addQueryParams(query)}`, "GET", params, null),
435445
}
436446

437447
}

0 commit comments

Comments
 (0)