Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Code generation from OpenAPI: TData (and others) are always void #279

Closed
thomas-eaa opened this issue Jul 7, 2020 · 3 comments
Closed

Comments

@thomas-eaa
Copy link

Describe the bug
First of all: many thanks for this great library, you've done a very good job! :)
We have a backend that is exposed via swagger and generate the API for the frontend, which is giving us the correct types and also functions, but the generic types TData, TError, etc. are always void, e.g.:
UseGetProps<void, void, void, void> or useGet<void, void, void, void>(...)

To Reproduce
Steps to reproduce the behavior:

  1. Generate code with this input:
{
   "swagger":"2.0",
   "info":{
      "description":"foo",
      "version":"API TOS",
      "title":"API",
      "termsOfService":"Terms of service",
      "contact":{
         "name":"Development",
         "url":"https://foo.bar",
         "email":"development@foo.bar"
      },
      "license":{
         "name":"MIT",
         "url":"https://foo.bar"
      }
   },
   "host":"localhost:8080",
   "basePath":"/",
   "tags":[
      {
         "name":"env-controller",
         "description":"Env Controller"
      }
   ],
   "paths":{
      "/api/env":{
         "get":{
            "tags":[
               "env-controller"
            ],
            "summary":"getEnv",
            "operationId":"getEnvUsingGET",
            "produces":[
               "*/*"
            ],
            "responses":{
               "200":{
                  "description":"OK",
                  "schema":{
                     "$ref":"#/definitions/EnvInfo"
                  }
               },
               "401":{
                  "description":"Unauthorized"
               },
               "403":{
                  "description":"Forbidden"
               },
               "404":{
                  "description":"Not Found"
               }
            }
         }
      }
   },
   "definitions":{
      "EnvInfo":{
         "type":"object",
         "properties":{
            "buildNumber":{
               "type":"string"
            },
            "environment":{
               "type":"string",
               "enum":[
                  "LOCAL",
                  "DEV",
                  "TEST",
                  "PREPROD",
                  "PROD",
                  "UNKNOWN"
               ]
            },
            "snapshot":{
               "type":"boolean"
            },
            "title":{
               "type":"string"
            },
            "version":{
               "type":"string"
            },
            "versionDetail":{
               "type":"string"
            }
         },
         "title":"EnvInfo"
      }
   }
}
  1. The generated code looks like this:
/* Generated by restful-react */

import React from "react";
import { Get, GetProps, useGet, UseGetProps } from "restful-react";

export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface EnvInfo {
  buildNumber?: string;
  environment?: "LOCAL" | "DEV" | "TEST" | "PREPROD" | "PROD" | "UNKNOWN";
  snapshot?: boolean;
  title?: string;
  version?: string;
  versionDetail?: string;
}

export type GetEnvUsingGETProps = Omit<GetProps<void, void, void, void>, "path">;

/**
 * getEnv
 */
export const GetEnvUsingGET = (props: GetEnvUsingGETProps) => (
  <Get<void, void, void, void>
    path={`/api/env`}
    {...props}
  />
);

export type UseGetEnvUsingGETProps = Omit<UseGetProps<void, void, void, void>, "path">;

/**
 * getEnv
 */
export const useGetEnvUsingGET = (props: UseGetEnvUsingGETProps) => useGet<void, void, void, void>(`/api/env`, props);

Expected behavior
We would expect that the response type (TData) is used in the generic definition of the types and functions.
In this example, EnvInfo should be used instead of void:

export type UseGetEnvUsingGETProps = Omit<UseGetProps<EnvInfo, void, void, void>, "path">;

/**
 * getEnv
 */
export const useGetEnvUsingGET = (props: UseGetEnvUsingGETProps) => useGet<EnvInfo, void, void, void>(`/api/env`, props);

At the moment we can work around this issue by defining the props and hooks ourselfs, but of course it would be nice if the generated API would be completely typed. Thank you!

@fabien0102
Copy link
Contributor

fabien0102 commented Jul 9, 2020

Hello,
Thanks the feedback, I played a bit locally with your specs and find the problem!

This is due to this part:

"produces":[
  "*/*"
],

We are not supporting (yet) the "wildcard pattern" (this is working if you put application/json instead of */*)

I'm pushing a fix right now to allow this pattern 😉

@fabien0102
Copy link
Contributor

v14.1.1 should do the trick 😉

@thomas-eaa
Copy link
Author

Works like a charm now - thank you so much! 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants