Swagger-codegen: Typescript-fetch does not implement Security definitions

Created on 12 Oct 2016  ·  22Comments  ·  Source: swagger-api/swagger-codegen

Description

Typescript-fetch does not implement in any way the ap+key or Oauth2 security definitions, either globally or indivually applied rules.

Swagger-codegen version

2.2.1

Swagger declaration file content or url

`````` yaml
swagger: '2.0'
info:
title: foo
description: bar
version: "1.0.0"
host: localhost:3001
schemes:

  • http

will be prefixed to all paths

basePath: /v1
produces:

  • application/json
    securityDefinitions:
    OpenSecurity:
    type: basic
    UserSecurity:
    type: apiKey
    in: header
    name: API-KEY
    security:
  • OpenSecurity: []
    paths:
    /foo:
    x-swagger-router-controller: Foo
    get:
    operationId: loadFoo
    summary: Loads foo foo
    description:
    foobar
    tags:
    - Foo
    parameters:
    - name: fooId
    in: query
    description: The foo id to load
    required: true
    type: string
    responses:
    200:
    description: An single foo that matched our search
    schema:
    type: array
    items:
    type: string
    default:
    description: Unexpected error
    schema:
    type: string
    post:
    operationId: submitFoo
    summary: Submit new Foo
    security:
    - UserSecurity: []
    description: |
    The consumer provides a foo to be submitted.
    tags:
    - Foo
    parameters:
    - name: foo
    in: body
    required: true
    description: The Foo to upload.
    schema:
    type: string
    responses:
    200:
    description: The submitted Foo
    schema:
    type: array
    items:
    type: string
    default:
    description: Unexpected error
    schema:
    type: string
    ```
Command line used for generation

``````

java -Dmodels -Dapis -DsupportingFiles=api.ts -jar
swagger-codegen-cli-2.2.1.jar generate -l typescript-fetch
-o ./src/api -i swagger.yaml --additional-properties supportsES6=true


TypeScript Feature

All 22 comments

@tht13 may I know if you've cycle to contribute the enhancements?

cc @leonyu, @Vrolijkx

I may be able too, if you could provide an example where this has been implemented.

I also noticed that typescript-fetch doesn't implement any parameters which are set in the header.

I think we can work together on this. I should have a PR to populate the header parameters shortly.

Yes, I'm copying the code over :)

Btw, I saw the handling of header parameters here in TS Fetch API template: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/api.mustache#L105-L114

Did you see similar code generated for your case?

@tht13 first attempt: https://github.com/swagger-api/swagger-codegen/pull/3983

please review :)

Disclaimer: no expert in Typescript

CI reported the following issues:

api.ts(31,31): error TS2307: Cannot find module 'configuration'.
api.ts(48,5): error TS2392: Multiple constructor implementations are not allowed.
api.ts(53,5): error TS2392: Multiple constructor implementations are not allowed.

I'm checking further.

I can double check later but I don't believe the haHeaderParam section generated, only the ^hasHeaderParam section. However the parameters were added to the method arguments and in the requirement checking section.

I will check your code in the next few hours, thanks for the help ☺️

I just pushed fixes to address the issues reported by the CI and the header parameter issue (should be hasHeaderParams instead of hasHeaderParam).

Please pull my branch to give it a try later.

Latest issues reported by the CI (partial result below):

  0 passing (25ms)
  12 failing

  1) PetApi should add and delete Pet:
     TypeError: Cannot read property 'accessToken' of undefined
      at Object.exports.PetApiFetchParamCreactor.addPet (node_modules/typescript-fetch-api/dist/api.js:71:31)
      at Object.exports.PetApiFp.addPet (node_modules/typescript-fetch-api/dist/api.js:318:58)
      at PetApi.addPet (node_modules/typescript-fetch-api/dist/api.js:492:33)
      at Context.<anonymous> (dist/test/PetApi.js:11:20)

  2) PetApi should get Pet by ID:
     TypeError: Cannot read property 'apiKey' of undefined
      at Object.exports.PetApiFetchParamCreactor.getPetById (node_modules/typescript-fetch-api/dist/api.js:187:31)
      at Object.exports.PetApiFp.getPetById (node_modules/typescript-fetch-api/dist/api.js:399:58)
      at PetApi.getPetById (node_modules/typescript-fetch-api/dist/api.js:525:33)
      at Context.<anonymous> (dist/test/PetApi.js:15:20)

ref: https://s3.amazonaws.com/archive.travis-ci.org/jobs/167303456/log.txt

Fixed in #4008 and #4005, Header params resoved in v2.2.2 and Security in v2.3.0

@tht13 thanks for your help in fixing this.

May I know if you've time to fix the same for typescript-angular, typescript-angular2 and typescript-node?

I'll put together a PR 👌(or 3)

On Thu, 20 Oct 2016, 15:01 wing328, [email protected] wrote:

@tht13 https://github.com/tht13 thanks for your help in fixing this.

May I know if you've time to fix the same for typescript-angular,
typescript-angular2 and typescript-node?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/swagger-api/swagger-codegen/issues/3970#issuecomment-255114277,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAx4cCxJnfo7JSs6_O-56i-suhuJaPLyks5q13QvgaJpZM4KU18Q
.

Regards,
Thomas Townsend
+47448960

Hi,

I thought this was still the case, but after deep diving into the mustache code it seems you guys took care of it. Great job!

BUT, I still don't seem to auth added to the output. My guess is that either {{authMethods}} or {{isApiKey}} evaluates to false.
do I need to configure something else other than adding something like the code below to the swagger.json? (I added both oauth and apiKey to test whether I was doing either wrong)

"securityDefinitions": {
    "Bearer": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header"
    },
    "Token": {
      "type": "oauth2",
      "flow": "accessCode"
    }
  },
  "security": {
    "Token": []
  }

Is it legitimate for the field name to be a string that is not a valid identifier in Typescript/Javascript?
I have a Swagger spec in which the name is "x-api-key" and the code that is currently generated by the template breaks because it tries to access it as configuration.apiKey.x-api-key.
Is the spec non-conforming? Or should these keys be handled in a more robust way?

@Aleksion in the endpoint, did you specify which security definition(s) to apply? e.g. https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L183

@ranma42 please open a new issue for tracking.

Btw, please also try the latest master and 2.3.0 to confirm whether the issue still occurs.

@wing238, i did yes. But when we changed to 2.3.0 it worked as it should.
Never got around to update it in this issue though. Sorry about that!

@Aleksion that's ok. Glad to hear that the issue is resolved.

Was this page helpful?
0 / 5 - 0 ratings