litmuschaos / litmus

Litmus helps SREs and developers practice chaos engineering in a Cloud-native way. Chaos experiments are published at the ChaosHub (https://hub.litmuschaos.io). Community notes is at https://hackmd.io/a4Zu_sH4TZGeih-xCimi3Q
https://litmuschaos.io
Apache License 2.0
4.45k stars 698 forks source link

Fix code generation for Chaos Center Authentication API #4537

Open smitthakkar96 opened 8 months ago

smitthakkar96 commented 8 months ago

While addressing issue #4236, I introduced a new endpoint to the Auth Server's capabilities. Following the steps in this documentation, I regenerated the swagger.json.

During frontend development, I noticed the auth client is generated using oats, but the oats config points to a manually generated swagger.json at a different location, not the auto-generated one. Switching to the auto-generated swagger.json led to several issues:

Encountered Issuess

Missing Version

This can be corrected by setting @Version here.

⠋ Generating spec for: authTypeError: Cannot read properties of undefined (reading 'startsWith')
    at loadSpecFromFileOrUrl (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/loadSpecFromFileOrUrl.mjs:58:42)
    at async importSpec (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/importSpec.mjs:63:30)
✖ Generating spec for: auth

Missing operationId

This can be corrected by adding @ID for every handler. Example

// FetchUsers       godoc
//
//  @Description    Fetch users.
//  @Tags           UserRouter
//  @Accept         json
//  @Produce        json
//  @Failure        401 {object}    response.ErrUnauthorized
//  @Failure        500 {object}    response.ErrServerError
//  @Success        200 {object}    response.UserResponse{}
//  @Router         /users [get]
//  @ID             users
⠋ Generating spec for: authError: Every path must have a operationId - No operationId set for post /auth/accept_invitation
    at file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/pathHelpers.mjs:38:23
    at Array.forEach (<anonymous>)
    at file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/pathHelpers.mjs:32:23
    at Array.forEach (<anonymous>)
    at processPaths (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/pathHelpers.mjs:29:38)
    at Object.generate (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-plugin-react-query/dist/generateReactQueryHooks.mjs:26:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async generateOpenAPISpec (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/generateOpenAPISpec.mjs:27:32)
    at async importSpec (file:///Users/s.thakkar/projects/litmus/chaoscenter/web/node_modules/@harnessio/oats-cli/dist/importSpec.mjs:63:30)
✖ Generating spec for: auth

After resolving these, I observed a huge diff in the client code, likely due to some responses not being composed of struct types and missing annotations on some structs.

Screenshot 2024-03-17 at 15 13 43

Proposed Solution

rootxrishabh commented 8 months ago

@smitthakkar96 I would like to work on this!

smitthakkar96 commented 8 months ago

@rootxrishabh thanks for expressing interest. I am not tackling this currently so please go ahead but I would suggest aligning with the maintainers first. You can also join the slack channel https://app.slack.com/client/T09NY5SBT/CNXNB0ZTN

cc: @Saranya-jena @neelanjan00 @namkyu1999

rootxrishabh commented 8 months ago

@smitthakkar96 I had joined the slack channel earlier. I will post my approach here before making a PR to get everyone's opinion. Sounds good?

smitthakkar96 commented 8 months ago

Yup sounds good

neelanjan00 commented 8 months ago

CC: @amityt can you please take a look?

rootxrishabh commented 7 months ago

Apologies folks, I currently don't have the bandwidth to complete this. Please feel free to take it up!

bhoopesh369 commented 5 months ago

I would like to work on this @Saranya-jena @neelanjan00 @namkyu1999

namkyu1999 commented 5 months ago

go ahead, @bhoopesh369

bhoopesh369 commented 5 months ago

IMG-20240610-WA0001.jpg

So, I had to add this @param annotation to reference an object for the request # body (entities.MemberInput). this has to be done for all structs.

But swagger was not able to parse it and throwed an error saying : 'cannot find type definition: entities.MemberInput'

So I looked into the official docs of swaggo/swag, The object we need to reference here is from the entities module which is present in the authentication/pkg dir It seems swag cannot parse files in outside dirs by default.

IMG-20240610-WA0002.jpg

So I had to include the --parseDependency flag in the swag init command, and it works

But this makes the 'swag init' go through unwanted files inside dependencies, thus taking little more time than previously it took.

Should I use the flag and continue ? OR Should I change the files/folder structure for it to be inside ?

@namkyu1999 @neelanjan00 @smitthakkar96 @Saranya-jena