Skip to content

Commit

Permalink
fix(cli): fix property schema mapping for openapi specs
Browse files Browse the repository at this point in the history
Fixes #5115
  • Loading branch information
raymondfeng committed Apr 16, 2020
1 parent 63a18c8 commit b851aed
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
22 changes: 21 additions & 1 deletion packages/cli/generators/openapi/spec-helper.js
Expand Up @@ -344,7 +344,27 @@ function buildMethodSpec(controllerSpec, op, options) {
names[argName] = 1;
}
registerAnonymousSchema([methodName, argName], paramSpec.schema, options);
const paramType = mapSchemaType(paramSpec.schema, options);
let propSchema = paramSpec.schema;
if (propSchema == null) {
/*
{
name: 'where',
in: 'query',
content: { 'application/json': { schema: [Object] } }
}
*/
const content = paramSpec.content;
const json = content && content['application/json'];
propSchema = json && json.schema;
if (propSchema == null && content) {
for (const m of content) {
propSchema = content[m].schema;
if (propSchema) break;
}
}
}
propSchema = propSchema || {type: 'string'};
const paramType = mapSchemaType(propSchema, options);
addImportsForType(paramType);
if (Array.isArray(_comments)) {
_comments.push(`@param ${argName} ${paramSpec.description || ''}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/generators/openapi/utils.js
Expand Up @@ -36,7 +36,7 @@ function isExtension(key) {
*/
function debugJson(msg, obj) {
if (debug.enabled) {
debug('%s: %s', msg, JSON.stringify(obj, null, 2));
debug('%s: %s', msg, util.inspect(obj, {depth: 10}));
}
}

Expand Down
Expand Up @@ -53,9 +53,12 @@ pulvinar elit eu, euismod sapien.
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
findPets(params: { tags: string[]; limit: number }): Promise<Pet[]>;
findPets(params: { tags: string[]; limit: number; where: {
[additionalProperty: string]: any;
} }): Promise<Pet[]>;
/**
* Creates a new pet in the store. Duplicates are allowed
Expand Down Expand Up @@ -249,9 +252,12 @@ pulvinar elit eu, euismod sapien.
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
findPets(params: { tags: string[]; limit: number }): Promise<Pet[]>;
findPets(params: { tags: string[]; limit: number; where: {
[additionalProperty: string]: any;
} }): Promise<Pet[]>;
/**
* Creates a new pet in the store. Duplicates are allowed
Expand Down Expand Up @@ -447,10 +453,13 @@ pulvinar elit eu, euismod sapien.
*
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
@operation('get', '/pets')
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number): Promise<Pet[]> {
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number, @param({name: 'where', in: 'query'}) where: {
[additionalProperty: string]: any;
}): Promise<Pet[]> {
throw new Error('Not implemented');
}
Expand Down Expand Up @@ -692,9 +701,12 @@ pulvinar elit eu, euismod sapien.
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
findPets(tags: string[], limit: number): Promise<Pet[]>;
findPets(tags: string[], limit: number, where: {
[additionalProperty: string]: any;
}): Promise<Pet[]>;
/**
* Creates a new pet in the store. Duplicates are allowed
Expand Down Expand Up @@ -890,10 +902,13 @@ pulvinar elit eu, euismod sapien.
*
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
@operation('get', '/pets')
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number): Promise<Pet[]> {
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number, @param({name: 'where', in: 'query'}) where: {
[additionalProperty: string]: any;
}): Promise<Pet[]> {
throw new Error('Not implemented');
}
Expand Down Expand Up @@ -1030,9 +1045,12 @@ pulvinar elit eu, euismod sapien.
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
findPets(tags: string[], limit: number): Promise<Pet[]>;
findPets(tags: string[], limit: number, where: {
[additionalProperty: string]: any;
}): Promise<Pet[]>;
/**
* Creates a new pet in the store. Duplicates are allowed
Expand Down
Expand Up @@ -55,10 +55,13 @@ pulvinar elit eu, euismod sapien.
*
* @param tags tags to filter by
* @param limit maximum number of results to return
* @param where
* @returns pet response
*/
@operation('get', '/pets')
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number): Promise<Pet[]> {
async findPets(@param({name: 'tags', in: 'query'}) tags: string[], @param({name: 'limit', in: 'query'}) limit: number, @param({name: 'where', in: 'query'}) where: {
[additionalProperty: string]: any;
}): Promise<Pet[]> {
throw new Error('Not implemented');
}
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/test/fixtures/openapi/3.0/petstore-expanded.yaml
@@ -1,4 +1,4 @@
openapi: "3.0.0"
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
Expand Down Expand Up @@ -39,6 +39,14 @@ paths:
schema:
type: integer
format: int32
- name: where
in: query
content:
'application/json':
schema:
type: object
title": TodoList.WhereFilter
additionalProperties: true
responses:
'200':
description: pet response
Expand Down Expand Up @@ -128,7 +136,7 @@ components:
allOf:
- $ref: '#/components/schemas/NewPet'
- required:
- id
- id
properties:
id:
type: integer
Expand All @@ -153,4 +161,3 @@ components:
format: int32
message:
type: string

Expand Up @@ -75,7 +75,10 @@ describe('openapi-generator with --client', function () {
});
});

it('generates files with --client for an existing datasource', async () => {
it('generates files with --client for an existing datasource', async function () {
// These tests take longer to execute, they used to time out on Travis CI
// eslint-disable-next-line no-invalid-this
this.timeout(10000);
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
Expand Down

0 comments on commit b851aed

Please sign in to comment.