litmus icon indicating copy to clipboard operation
litmus copied to clipboard

Fix code generation for Chaos Center Authentication API

Open smitthakkar96 opened this issue 1 year ago • 9 comments

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

  • [ ] Add missing version information
  • [ ] Add @ID for all routes
  • [ ] Annotate response structs

smitthakkar96 avatar Mar 17 '24 14:03 smitthakkar96

@smitthakkar96 I would like to work on this!

rootxrishabh avatar Mar 19 '24 08:03 rootxrishabh

@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

smitthakkar96 avatar Mar 19 '24 08:03 smitthakkar96

@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?

rootxrishabh avatar Mar 19 '24 09:03 rootxrishabh

Yup sounds good

smitthakkar96 avatar Mar 19 '24 10:03 smitthakkar96

CC: @amityt can you please take a look?

neelanjan00 avatar Mar 19 '24 15:03 neelanjan00

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

rootxrishabh avatar Apr 03 '24 05:04 rootxrishabh

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

bhoopesh369 avatar Jun 04 '24 09:06 bhoopesh369

go ahead, @bhoopesh369

namkyu1999 avatar Jun 04 '24 09:06 namkyu1999

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

bhoopesh369 avatar Jun 09 '24 20:06 bhoopesh369