简体   繁体   中英

Node.js - Swagger - Unable to render this definition

I am following this toutorial: https://github.com/codeBelt/open-api-documentation/blob/master/src/openApiDocumentation.js Can I validate my openApiDocumentation.js file somewhow? I get:

Unable to render this definition The provided definition does not specify a valid version field. Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

I am attaching mi.js file. Maybe you guys will see a typo here. Thanks in advance.

.js file:

const USER_TYPES = {
    EXCHANGE: 'xxx',
    GIVEAWAY: 'xxx'
}
const openApiDocumentation = {
    openapi: '3.0.1',
    info: {
        version: '1.3.0',
        title: 'xxx',
        description: 'xxx',
        contact: {
            name: 'xxx',
            email: 'xxx',
        }
    },
    license: {
        name: 'Apache 2.0',
        url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
    },
    servers: [
        {
            url: 'http://localhost:4000/',
            description: 'Local server',
        },
    ],
    tags: [
        {
            name: 'Books CRUD operations',
        },
    ],
    paths: {
        '/findAllBooks': {
            get: {
                tags: ['CRUD operations'],
                description: 'Get all Book offers',
                operationId: 'getUsers',
                parameters: [
                    {
                        name: 'page',
                        in: 'query',
                        schema: {
                            type: 'integer',
                            default: 1,
                        },
                        required: true,
                        description: 'Page numer used pagination.',
                    },
                ],
                responses: {
                    '200': {
                        description: 'Books were obtained',
                        content: {
                            'application/json': {
                                schema: {
                                    $ref: '#/components/schemas/Books',
                                },
                            },
                        },
                    },
                    '500': {
                        description: 'Missing parameters',
                        content: {
                            'application/json': {
                                schema: {
                                    $ref: '#/components/schemas/Error',
                                },
                                example: {
                                    message: 'page qyery parameter is missing',
                                    internal_code: 'missing_parameters',
                                },
                            },
                        },
                    },
                },
            },
        },
    },
    components: {
        schemas: {
            coverImg: {
                type: 'string',
                example: 'http:',
            },
            image: {
                type: 'string',
                example: 'http',
            },
            category: {
                type: 'string',
                example: 'Crafts & Hobbies',
            },
            linkTypes: {
                type: 'object',
                properties: {
                    coverImg: {
                        $ref: '#/components/schemas/coverImg',
                    },
                    images: {
                        type: 'array',
                        items: {
                            $ref: '#/components/schemas/image',
                        },
                    }
                }
            },
            offerID: {
                type: 'string',
                example: '27301927',
            },
            userID: {
                type: 'string',
                example: 'efdc5192',
            },
            title: {
                type: 'string',
                example: 'Quilting For Dummies',
            },
            description: {
                type: 'string',
                example: 'You ',
            },
            categories: {
                type: 'array',
                items: {
                    $ref: '#/components/schemas/category',
                },
            },
            links: {
                type: 'object',
                items: {
                    $ref: '#/components/schemas/linkTypes',
                },
            },
            offerType: {
                type: 'string',
                enum: USER_TYPES,
                default: USER_TYPES.EXCHANGE,
            },
            Book: {
                type: 'object',
                properties: {
                    offerID: {
                        $ref: '#/components/schemas/offerID',
                    },
                    userID: {
                        $ref: '#/components/schemas/userID',
                    },
                    title: {
                        $ref: '#/components/schemas/title',
                    },
                    description: {
                        $ref: '#/components/schemas/description',
                    },
                    categories: {
                        $ref: '#/components/schemas/categories',
                    },
                    imageLinks: {
                        $ref: '#/components/schemas/links',
                    },
                    offerType: {
                        $ref: '#/components/schemas/offerType',
                    },
                },
            },
            Books: {
                type: 'object',
                properties: {
                    users: {
                        type: 'array',
                        items: {
                            $ref: '#/components/schemas/Book',
                        },
                    },
                },
            },
            Error: {
                type: 'object',
                properties: {
                    message: {
                        type: 'string',
                    },
                    internal_code: {
                        type: 'string',
                    },
                },
            },
        },
    },
};

There are few mistakes,

  • license must be inside the info object
info: {
    version: '1.3.0',
    title: 'xxx',
    description: 'xxx',
    contact: {
        name: 'xxx',
        email: 'xxx' // make sure you have used valid email address!
    },
    license: {
        name: 'Apache 2.0',
        url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
    }
}
  • enum will allow only array not an object and here you have passed object USER_TYPES , corrected below:
const USER_TYPES = {
    EXCHANGE: 'xxx',
    GIVEAWAY: 'xxx'
};

const USER_TYPES_ENUM = [
    USER_TYPES.EXCHANGE,
    USER_TYPES.GIVEAWAY
];
offerType: {
    type: 'string',
    enum: USER_TYPES_ENUM,
    default: USER_TYPES.EXCHANGE,
},

For best practice use https://editor.swagger.io/ (also they have provided a option to convert json to yaml under Edit > Convert to YAML ) !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM