Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const API = require('../spec/openapi.json');
- const findOperation = (operationId) => {
- const { paths } = API;
- for (let url in paths) {
- const path = paths[url];
- for (let method in path) {
- const operation = path[method];
- if (operation.operationId === operationId) return operation;
- }
- }
- return false;
- };
- const findSchema = (schemaName) => {
- const { components: { schemas } } = API;
- for (let schemaKey in schemas) {
- if (schemaKey === schemaName) return schemas[schemaKey];
- }
- return false;
- };
- const getTransformedSchema = (schema) => {
- const transformedSchema = {
- properties: {},
- required: []
- };
- for (let key in schema.properties) {
- const property = schema.properties[key];
- transformedSchema.properties[key] = {
- type: property.type
- };
- }
- for (let key in schema.required) {
- transformedSchema.required.push(schema.required[key]);
- }
- return transformedSchema;
- };
- /**
- * Parses API specification and tries to find Schema by operationId
- * @param operationId
- * @return {boolean|Object}
- */
- const fromRequestBody = (operationId) => {
- const operation = findOperation(operationId);
- if (
- !operation ||
- !operation['requestBody'] ||
- !operation['requestBody']['content'] ||
- !operation['requestBody']['content']['application/json'] ||
- !operation['requestBody']['content']['application/json']['schema'] ||
- !operation['requestBody']['content']['application/json']['schema']['properties']
- ) return false;
- const schema = operation['requestBody']['content']['application/json']['schema'];
- return getTransformedSchema(schema);
- };
- /**
- * Parses API specification and tries to find Schema by schemaName
- * @param schemaName
- * @return {boolean|Object}
- */
- const fromComponentsSchemas = (schemaName) => {
- const schema = findSchema(schemaName);
- if (
- !schema
- ) return false;
- return getTransformedSchema(schema);
- };
- module.exports = {
- fromRequestBody,
- fromComponentsSchemas
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement