openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
550 stars 67 forks source link

Client.operationId generates GET instead of POST request #129

Closed mwaeckerlin closed 1 year ago

mwaeckerlin commented 1 year ago

I have a nasty bug using openapi-client-axios in react-openapi-axios:

    const { api } = useContext(OpenAPIContext)
    const { data, error, loading, response } = useOperation('getUsers') // works as expected
    return (
        <>
            {!done && <Form
                heading="Create New User"
                ready={!!password && !!name && !done}
                ok={() => {
                    api.getClient().then(client => {
                        console.log('CREATE-USER', { client })
                        // the following line works as expected
                        client.paths['/user'].post(null, { id: name, key: password })
                        // the following line creates a GET request instead of a POST request
                        //client.createUser(null, { id: name, key: password })
                    })
                }}
            >

The relevant part of the API-JSON (full file attached in api-json.log):

  "paths": {
    "/user": {
      "post": {
        "operationId": "createUser",
        "summary": "",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateUserDto"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": ""
          }
        },
        "tags": [
          "user"
        ]
      },

The API is generated in NestJS by the controller specification:

@Controller('user')
@ApiTags('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post()
  @ApiOperation({operationId: 'createUser'})
  async create(@Body() createUserDto: CreateUserDto): Promise<void> {
    await this.userService.create(createUserDto)
  }

  @Get()
  @ApiOperation({operationId: 'getUsers'})
  async findAll() {
    return await this.userService.findAll()
  }

  @Get(':id')
  @ApiOperation({operationId: 'createUser'})
  async findOne(@Param('id') id: string) {
    return await this.userService.findOne(id)
  }

  @Patch(':id')
  @ApiOperation({operationId: 'updateUser'})
  async update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return await this.userService.update(id, updateUserDto)
  }

  @Delete(':id')
  @ApiOperation({operationId: 'deleteUser'})
  async remove(@Param('id') id: string) {
    return await this.userService.remove(id)
  }
}

Calling client.createUser(null, { id: name, key: password }) generates a GET request with parameter undefined instead of a POST request: image

Calling client.paths['/user'].post(null, { id: name, key: password }) works as expected.

anttiviljami commented 1 year ago

(full file attached in api-json.log):

Hi @mwaeckerlin. Looks like the createUser operation is duplicated in the openapi definition you linked