Skip to content
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

useClassTransformer doesn't work when modelMode is set to interface #80

Closed
aranoe opened this issue Feb 17, 2020 · 6 comments · Fixed by #83
Closed

useClassTransformer doesn't work when modelMode is set to interface #80

aranoe opened this issue Feb 17, 2020 · 6 comments · Fixed by #83

Comments

@aranoe
Copy link

aranoe commented Feb 17, 2020

Problem Statement:
As mentioned in #32 it is desirable to convert date types (date and date-time) to Date object instead of string. This currently only works with modelMode: "class". Setting modelMode: "interface" results in an error.

How to reproduce:
Run swagger-axios-codegen with following configuration:

const { codegen } = require("swagger-axios-codegen");

codegen({
  methodNameMode: "operationId",
  remoteUrl: "https://petstore.swagger.io/v2/swagger.json", 
  useClassTransformer: true,
  outputDir: "./out",
  modelMode: "interface",
  useStaticMethod: true
});

Example Error:

Argument of type 'unknown[]' is not assignable to parameter of type 'Pet | PromiseLike | undefined'.
Property 'then' is missing in type 'unknown[]' but required in type 'PromiseLike'

In addition to that, dates are not transformed to Date:

// https://petstore.swagger.io/v2/swagger.json
...
  "Order": {
    ...
     "shipDate": {
       "type": "string",
       "format": "date-time"
     }
    ...
  }
...
// generated index.ts
export interface Order {
 ...
  shipDate: string; // should be Date
 ...
}
@Manweill
Copy link
Owner

@aranoe
useClassTransformer is only work in modelMode='class'

@aranoe
Copy link
Author

aranoe commented Feb 17, 2020

@Manweill I think this feature makes perfectly sense for interface models as well. If it's not possible to do with useClassTransformer another approach is desired.
Maybe a configuration name like useStringForDates with default value true would be suitable.

@Manweill
Copy link
Owner

Can add a setting to format the response result with the format in the swagger.json specification.

@aranoe
Copy link
Author

aranoe commented Feb 17, 2020

@Manweill The format is already set to date-time as shown in the opening post. The problem is, that swagger-axios-codegen still transforms this to a string type. But the desired type would/can be Date too. This is already possible with class models (useClassTransformer), but not with interfaces.

@aranoe
Copy link
Author

aranoe commented Feb 17, 2020

I just looked at your code and saw that it should already produce Date by default, when "type": "string" and "format": "date" /"format": "date-time"

https://github.com/aranoe/swagger-axios-codegen/blob/cb7bf85fe0f7db69b0131152b8f6cb64d9aff922/src/utils.ts#L86-L98

This seems like a bug now.

@Manweill can you confirm, when you run swagger-axios-codegen with the above configuration, that it also produces string instead of Date for shipDate in the Order model? I tested it multiple times now, with the same result.

@Manweill
Copy link
Owner

Manweill commented Feb 18, 2020

@aranoe
Emm,I haven't actually used this feature, so you may need to discuss it with @arkraft or you can submit your new idea.
Welcome PR.

aranoe pushed a commit to aranoe/swagger-axios-codegen that referenced this issue Feb 22, 2020
Manweill pushed a commit that referenced this issue Feb 23, 2020
fix: pass format value for modeMode: "interface", which was null #80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants