-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when generating parameters by reference #53
Comments
@danielgelling |
When we add a parameter to our spec, and reference this to this parameter in the path specification the generator gives the error listed above. Part of our spec.json {
"paths": {
"/users": {
"get": {
"summary": "Returns a list of users",
"operationId": "someOperation",
"// ... and the other values usually in the spec": "",
"parameters": [
{
"$ref": "#/parameters/someParam"
}
]
}
}
},
"parameters": {
"someParam": {
"name": "paramNameAsUsedByClient",
"in": "query",
"required": false,
"description": "A description of this parameter",
"type": "string"
}
}
}
If you need any more information, or access to our spec, just let me know :-) |
The |
The spec.json file is generated and is Swagger 2.0 compliant. |
@haroldiedema "/products-test": {
"get": {
"tags": [
"products-test"
],
"summary": "Product Types",
"description": "The Products endpoint returns information about the *Uber* products\noffered at a given location. The response includes the display name\nand other details about each product, and lists the products in the\nproper display order.\n",
"parameters": [
{
"name": "latitude",
"in": "query",
"description": "Latitude component of location.",
"required": true,
"type": "number",
"format": "double"
},
{
"name": "longitude",
"in": "query",
"description": "Longitude component of location.",
"required": true,
"type": "number",
"format": "double"
}
], |
This is only when we globally define the parameter in the spec, and then reference to it in the route. This is so you don't have to copy/paste the parameters for each route that use that same parameter. Other parameters that are route specific are defined in the parameters in the route itself, like in your example. Which obviously does work (: |
To put this use-case into context: We have a specific parameter: The |
Currently, you can define the parameter in definition. |
@danielgelling |
@Manweill I just generated a new spec.json (only with two of our endpoints, one of which uses a parameter by reference, the other has its parameters specified in the route itself). You can find it here: https://gist.github.com/danielgelling/d816b09e76bf662723776ab07de1dd90 Using this generator it gives the error listed above, however when using the same spec with the official swagger-codegen-cli it just works as expected. |
Indeed, using parameters as a reference is quite common in swagger generated docs. I was going to use your library for our new E2E framework, but i'm glad that i found this issue first :) Would love to see this feature in the future :) |
@Manweill any ETA when this will be fixed? I want to use this code-gen in my project but i am blocked due to this error. Almost all of our swaggers has params as "$ref". |
@usman087 |
|
@Manweill You support Models (definitions) being referenced via For example, alot of your end-points use the same parameter. It is useful for these parameters to be defined once in your application, so you can simply reference them instead of copy-pasting them repeatedly. The official swagger-codegen-cli supports this just fine. I'm sorry, but I'm not able to explain this issue any more clearer. |
@haroldiedema |
@Manweill No our |
@danielgelling |
There is a similar issue with using $ref for request bodies (node:19348) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'multipart/form-data' of undefined Ref is not resolved and the code tries to look for _a.content['multipart/form-data'] which of course fails. |
@mikaelnilsson77 |
"openapi": "3.0.2", |
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
});
}
},
); |
@MartinLoeper Emmm.... and then? |
Uhm, let me elaborate: I was working on a customer's OData API that provided a swagger documentation (OpenAPI 3.0.0) and I wanted to auto-generate a typescript client using this package. Unfortunately, it produced incorrect parameters as the swagger spec used 'ref' for the common OData parameters such as: To make it work, I decided that it is easier to change the input for swagger-axios-codegen than the package's implementation. I guess, in the long run we could also integrate json-schema-ref-parser into this package to support the kind of references that the thread opener described. |
@mikaelnilsson77 it's great. |
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.The text was updated successfully, but these errors were encountered: