Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client generation with required sanitization #3

Merged
merged 15 commits into from
Nov 1, 2024

Conversation

rahul1995
Copy link
Contributor

@rahul1995 rahul1995 commented Oct 26, 2024

Purpose

To create the OpenAPI spec, sanitize it to make compatible with Ballerina OpenAPI tool, and generate Client using it.

Issue Title: Introduce a Ballerina connector for Dropbox REST API
Issue link: ballerina-platform/ballerina-library#7054

Examples

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

ballerina/Ballerina.toml Outdated Show resolved Hide resolved
@rahul1995 rahul1995 requested a review from Nuvindu October 27, 2024 00:21
@lnash94
Copy link
Member

lnash94 commented Oct 28, 2024

@rahul1995, This generated OpenAPI looks good 😃, Here I have noticed a few improvements we can add.

  • Given that the OpenAPI specification has a lot of inline schemas for particular request bodies and responses, shall we move them to named schemas under the components section, define names that properly align with Ballerina record naming best practices, and refer to them in the relevant places?

ex: some sample for the inline schema

"requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "photo": {
                    "type": "object",
                    "properties": {
                      ".tag": {
                        "type": "string",
                        "example": "base64_data"
                      },
                      "base64_data": {
                        "type": "string",
                        "example": "SW1hZ2UgZGF0YSBpbiBiYXNlNjQtZW5jb2RlZCBieXRlcy4gTm90IGEgdmFsaWQgZXhhbXBsZS4="
                      }
                    }
                  }
                }
              },

After modified ,
the inline schema need to be sent under the component.schemas section with named object and reference it into relevant requestBody or response in operation.

"component": {
          "schemas": {
            "ProfilePhoto": {
              "schema": {
                "type": "object",
                "properties": {
                  "photo": {
                    "type": "object",
                    "properties": {
                      ".tag": {
                        "type": "string",
                        "example": "base64_data"
                      },
                      "base64_data": {
                        "type": "string",
                        "example": "SW1hZ2UgZGF0YSBpbiBiYXNlNjQtZW5jb2RlZCBieXRlcy4gTm90IGEgdmFsaWQgZXhhbXBsZS4="
                      }
                    }
                  }
                }
              },
...
"requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/component/schemas/ProfilePhoto"
                    }
                  }
                }
              },

ballerina/client.bal Outdated Show resolved Hide resolved
@rahul1995
Copy link
Contributor Author

@lnash94 Thanks for the review. I agree with your comment, but this is like 35k lines of json file. It's looking infeasible for me to finish it as an individual. Can I improve some main API endpoints for now and take this as a task for later (distributed among other folks if possible)?

…ox-api-select-admin` parameters and reuse across API endpoints
@lnash94
Copy link
Member

lnash94 commented Oct 28, 2024

@lnash94 Thanks for the review. I agree with your comment, but this is like 35k lines of json file. It's looking infeasible for me to finish it as an individual. Can I improve some main API endpoints for now and take this as a task for later (distributed among other folks if possible)?

Hi @rahul1995,

Yeah, it is understandable that this has a lot of endpoints. Until we provide the set of operations, I can bring some workarounds to you to address the pain points here.
Workaround:

  1. Instead of the JSON format, let's use YAML format to increase readability and reduce unnecessary line counts. You can use this Swagger editor: https://editor-next.swagger.io/ to convert it into YAML format.
  2. Then, you can use the same editor to generate a server or client using OpenAPI as the language. It will generate the YAML and automatically move inline objects into named records (you can use any preferred method here).
  3. Once you receive the YAML, you can edit the name references by giving them meaningful names that align with Ballerina best practices. ex: inline_response_200_73_event_category -> EventCategoryResponse

Please Note: during every modification lets check the modified details in the API is align with the original API documentation 😊.
Hope this workaround helps alleviate the issues you're experiencing. If you have any questions or need additional assistance, please don't hesitate to reach out to us. We're here to help 🙌 !

@rahul1995
Copy link
Contributor Author

@lnash94 Thanks for sharing this method. It will save a lot of time.
I see that there are ~500 named schemas which I need to rename because many names are like inline_response_xxx, and others are not following the best practices.

Since these many will take a while, I have created a commit with a few renamed so that I get an early review on this.
Could you please review?

@lnash94
Copy link
Member

lnash94 commented Oct 29, 2024

@lnash94 Thanks for sharing this method. It will save a lot of time. I see that there are ~500 named schemas which I need to rename because many names are like inline_response_xxx, and others are not following the best practices.

Since these many will take a while, I have created a commit with a few renamed so that I get an early review on this. Could you please review?

Yeah sure , I will review :)

@rahul1995 rahul1995 requested a review from lnash94 October 29, 2024 03:37
docs/spec/openapi.yaml Outdated Show resolved Hide resolved
docs/spec/openapi.yaml Outdated Show resolved Hide resolved
docs/spec/openapi.yaml Outdated Show resolved Hide resolved
docs/spec/openapi.yaml Outdated Show resolved Hide resolved
docs/spec/openapi.yaml Outdated Show resolved Hide resolved
@lnash94
Copy link
Member

lnash94 commented Oct 29, 2024

@rahul1995, avoiding using verbs as prefixes in record names is generally recommended. The best practice is to focus on the state or data the record represents rather than the action performed on it.

Shall we simplify the object name by minimizing the action verb for clarity and alignment with Ballerina's name?

ex: AddPropertiesRequest -> NewPropertiesRequest, PropertiesRequest

Please check and apply this to other relevant places.

@rahul1995
Copy link
Contributor Author

rahul1995 commented Oct 29, 2024

@lnash94 I have made it like this only, it is that sometimes a particular API endpoint requires a specific format of request, that is where I am also creating records specific to that request body and naming it with verb. For example, I first created a record/schema Photo and then I created SetProfilePhotoRequest using Photo schema to define the format SetProfilePhoto API expects.

I have used the same practices as done in https://github.com/ballerina-platform/module-ballerinax-twitter/blob/main/docs/spec/openapi.json

@rahul1995 rahul1995 requested a review from lnash94 October 29, 2024 03:56
@rahul1995
Copy link
Contributor Author

Hey @lnash94 thanks for the review :) I cleaned up a little bit and added PRs for tests, examples, and documentations.

For the Schema naming, the current strategy is also in line with the Dropbox's official documentation, i.e. Dropbox also uses same naming conventions (verb + API Name + Arg/Result). For example, AddTemplateResult as shown in the screenshot attached for the /templates/add_for_user API.

Please let me know how we should proceed further. 😊
Screenshot 2024-10-30 at 12 27 59 AM

@lnash94
Copy link
Member

lnash94 commented Oct 30, 2024

Hi @rahul1995

Hey @lnash94 thanks for the review :) I cleaned up a little bit and added PRs for tests, examples, and documentations.

For the Schema naming, the current strategy is also in line with the Dropbox's official documentation, i.e. Dropbox also uses same naming conventions (verb + API Name + Arg/Result). For example, AddTemplateResult as shown in the screenshot attached for the /templates/add_for_user API.

Please let me know how we should proceed further. 😊 Screenshot 2024-10-30 at 12 27 59 AM

Let's proceed with the process (verb + API Name + Arg/Result) , it is fine :) . lets follow the same thing which has been done in Dropbox.

@rahul1995
Copy link
Contributor Author

Thanks @lnash94. I have exactly matched these names as given in the documentation. Please review.

Also, I am open to contribute further outside of Hacktoberfest if needed :)
Learned a lot about OpenAPI during this task.

@rahul1995 rahul1995 force-pushed the client-generation branch 3 times, most recently from 55455e5 to c299ce9 Compare October 30, 2024 09:57
docs/spec/sanitations.md Outdated Show resolved Hide resolved
Co-authored-by: Sumudu Nissanka <lnash94@users.noreply.github.com>
@rahul1995 rahul1995 requested a review from lnash94 October 31, 2024 12:34
Copy link
Contributor

@NipunaRanasinghe NipunaRanasinghe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lnash94 lnash94 merged commit bf017b2 into ballerina-platform:main Nov 1, 2024
2 checks passed
@rahul1995 rahul1995 deleted the client-generation branch November 12, 2024 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants