contiamo / restful-react

A consistent, declarative way of interacting with RESTful backends, featuring code-generation from Swagger and OpenAPI specs 🔥
MIT License
1.87k stars 109 forks source link

Generating a client with path params containing dots '.' fails #377

Open willycoll opened 2 years ago

willycoll commented 2 years ago

Describe the bug clients generated from swagger.json with path parameters containing dots . contain errors

To Reproduce Steps to reproduce the behavior:

{
  "swagger": "2.0",
  "info": {
    "title": "contiamo/restful-react bug",
    "version": "0.0.0"
  },
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/foo/{bar.baz}": {
      "get": {
        "operationId": "foo",
        "responses": {
          "200": {
            "description": "A successful response."
          },
          "default": {
            "description": "An unexpected error response."
          }
        },
        "parameters": [
          {
            "name": "bar.baz",
            "in": "query",
            "required": false,
            "type": "string"
          }
        ]
      }
    }
  }
}

1) restful-react import --file swagger.json --output client.tsx 2) That generates the following invalid tsx:

/* Generated by restful-react */

import React from "react";
import { Get, GetProps, useGet, UseGetProps } from "restful-react";
export const SPEC_VERSION = "0.0.0"; 

export interface FooQueryParams {
  "bar.baz"?: string;
}

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

export const Foo = (props: FooProps) => (
  <Get<void, void, FooQueryParams, void>
    path={`/foo/${bar.baz}`}

    {...props}
  />
);

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

export const useFoo = (props: UseFooProps) => useGet<void, void, FooQueryParams, void>(`/foo/${bar.baz}`, props);

Expected behavior A valid client.tsx file

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

fabien0102 commented 2 years ago

To be totally honest, I’m surprised that this is valid 😅 But indeed, good catch!