swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.07k stars 6.03k forks source link

generating a java client with OAuth2 client_credentials flow support #7733

Open reggie-k opened 6 years ago

reggie-k commented 6 years ago
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

hiroshan commented 3 months ago

is this fixed now , if so how can we achieve this