swagger-axios-codegen: Error when generating parameters by reference

When specifying a global parameter in documentation.parameters and referencing to this parameter in the request ('$ref': '#/parameters/someParam') specification an error occurs when generating the client.

(node:93171) UnhandledPromiseRejectionWarning: Error: Request failed with status code 500
    at createError (/path/to/project/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/path/to/project/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/path/to/project/node_modules/axios/lib/adapters/http.js:237:11)
    at IncomingMessage.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1183:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:93171) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:93171) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.

Hash: 7532bd6d1996f82068cf
Version: webpack 4.35.0
Time: 31ms
Built at: 10/16/2019 11:54:09 AM

ERROR in Entry module not found: Error: Can't resolve './our-project.js' in '/path/to/project/bin/build'

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (14 by maintainers)

Most upvoted comments

We were able to work around this issue using https://github.com/APIDevTools/json-schema-ref-parser and passing the dereferenced swagger definition to codegen.

Note: We published a more recent version of json-schema-ref-parser that ships the feature to exclude certain paths from being dereferenced. No clue why they do not publish it upstream.

Example:

import { codegen } from "swagger-axios-codegen";
import * as $RefParser from "@nesto-software/json-schema-ref-parser";
import * as fs from "fs";
import * as path from "path";

// eslint-disable-next-line
$RefParser.dereference(require("../resources/swagger.json"), {
        dereference: {
            circular: "ignore",

            // note: we must not dereference responses, otherwise no dtos are created
            //       this is the most hilarious workaround ever
            //       now we basically inlining parameters etc. but not response-related paths
            excludedPathMatcher: path => path.includes("/get/responses/") || path.includes("/post/responses/") || path.includes("/properties/value/"),
        },
    },
    (err, fixedSchema) => {
        if (err) {
            console.error(err);
            return;
        } else {
            // for debugging purposes: save the dereferenced schema
            fs.writeFileSync(path.join(__dirname, "../resources/swagger-dereferenced.json"), JSON.stringify(fixedSchema, null, 2));

            codegen({
                methodNameMode: "operationId",
                source: fixedSchema,
                outputDir: "./lib/api/",
                fileName: "services.ts",
                useStaticMethod: false,
                useHeaderParameters: false,
                modelMode: "class",
                useClassTransformer: true
            });
        }
    },
);

Currently, you can define the parameter in definition. And then, welcome PR or wait for me of next version .