swagger-api / swagger-api/swagger-codegen

generating a java client with OAuth2 client_credentials flow support

Open
#7,733 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When I generate the java client for -library=resttemplate, I can inject the OAUth2 resttemplate into the generated ApiClient, and do not need to worry about the dealing with the tokens.
When I generate the java client with no libraries (and the swagger file of the API contains the relevant section for OAuth securityScheme, including the tokenUrl), the generated ApiClient does not include any logic of OAuth flow. The api is secured with ClientCredentials grant.

The only reference the generated ApiClient has to Auth is:
`

   // Setup authentications (key: authentication name, value: authentication).

    authentications = new HashMap<String, Authentication>();

    authentications.put("oauth", new OAuth());`
Swagger-codegen version

2.3.1

Swagger declaration file content or url

{

            "swagger":"2.0",

            "info":{

                            "description":"This API provides account and transaction operations. DEMO",

                            "version":"1.0.0",

                            "title":"Accounts REST API",

                            "termsOfService":"Terms of service",

                            "contact":{},"license":{

                                            "name":"MIT","url":"MIT_LICENSE_URL"}

                            },

            "securityDefinitions": {

    "oauth": {

        "type": "oauth2",

        "tokenUrl": "https://kuku.muku.buku:1234/token",

        "flow": "application",

                                            "scopes": {

                                                            "read": "read all"

                                            }

    }

},

            "security": [

                            {              "oauth": ["read"] }

            ],



            "host":"kuku.muku.buku.chmuku","basePath":"/",

            "tags":

            [{

                            "name":"transaction-controller",

                            "description":"Transaction Controller"

            },

            {

                            "name":"account-controller",

                            "description":"Account Controller"

            }

            ],

            "paths":

            {

            "/accounts":

            {"get":{"tags":["account-controller"],"summary":"findByName","operationId":"findByNameUsingGET","consumes":["application/json"],"produces":["*/*"],"parameters":[{"name":"name","in":"query","description":"name","required":false,"type":"string"}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/Account"}}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}},"post":{"tags":["account-controller"],"summary":"createAccount","operationId":"createAccountUsingPOST","consumes":["application/json"],"produces":["*/*"],"parameters":[{"in":"body","name":"account","description":"account","required":true,"schema":{"$ref":"#/definitions/Account"}}],"responses":{"200":{"description":"OK","schema":{"type":"string"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}},"put":{"tags":["account-controller"],"summary":"updateAccount","operationId":"updateAccountUsingPUT","consumes":["application/json"],"produces":["*/*"],"parameters":[{"in":"body","name":"account","description":"account","required":true,"schema":{"$ref":"#/definitions/Account"}}],"responses":{"200":{"description":"OK","schema":{"type":"string"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}},"/accounts/transactions":{"get":{"tags":["transaction-controller"],"summary":"findAll","operationId":"findAllUsingGET","consumes":["application/json"],"produces":["*/*"],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/TransactionDTO"}}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}},"post":{"tags":["transaction-controller"],"summary":"makeTransaction","operationId":"makeTransactionUsingPOST","consumes":["application/json"],"produces":["*/*"],"parameters":[{"in":"body","name":"transactionDTO","description":"transactionDTO","required":true,"schema":{"$ref":"#/definitions/TransactionDTO"}}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/TransactionDTO"}}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}},"/accounts/transactions/{transactionId}":{"get":{"tags":["transaction-controller"],"summary":"findById","operationId":"findByIdUsingGET_1","consumes":["application/json"],"produces":["*/*"],"parameters":[{"name":"transactionId","in":"path","description":"transactionId","required":true,"type":"string"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/TransactionDTO"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}},"/accounts/{accountId}":{"get":{"tags":["account-controller"],"summary":"findById","operationId":"findByIdUsingGET","consumes":["application/json"],"produces":["*/*"],"parameters":[{"name":"accountId","in":"path","description":"accountId","required":true,"type":"integer","format":"int32"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/Account"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}},"delete":{"tags":["account-controller"],"summary":"deleteAccount","operationId":"deleteAccountUsingDELETE","consumes":["application/json"],"produces":["*/*"],"parameters":[{"name":"accountId","in":"path","description":"accountId","required":true,"type":"integer","format":"int32"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/Account"}},"204":{"description":"No Content"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"}}}},"/accounts/{accountId}/transactions":{"get":{"tags":["transaction-controller"],"summary":"findByAccount","operationId":"findByAccountUsingGET","consumes":["application/json"],"produces":["*/*"],"parameters":[{"name":"accountId","in":"path","description":"accountId","required":true,"type":"integer","format":"int32"}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/TransactionDTO"}}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}}},"definitions":{"Account":{"type":"object","properties":{"address":{"type":"string"},"balance":{"type":"number"},"creationDate":{"type":"string","format":"date-time"},"email":{"type":"string"},"fullName":{"type":"string"},"id":{"type":"integer","format":"int32"},"userId":{"type":"string"}}},"TransactionDTO":{"type":"object","properties":{"amount":{"type":"number"},"date":{"type":"string","format":"date-time"},"description":{"type":"string"},"fromAccountId":{"type":"integer","format":"int32"},"id":{"type":"string"},"toAccountId":{"type":"integer","format":"int32"}}}}}
Command line used for generation

java -jar swagger-codegen-cli-2.3.1.jar generate -l java -o AccountServiceClientOauth -i
swagger.json --group-id com.infra.samples --artifact-id account-service-client-oauth

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the swagger.json securityDefinitions and the Java client generated by the stated swagger-codegen CLI command, especially its ApiClient and OAuth authentication references. Compare this output with generation using -library=resttemplate; done means the no-library Java client handles the declared OAuth2 application/client_credentials flow rather than only exposing an OAuth object.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, authentication
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.