Azure / autorest.typescript

Extension for AutoRest (https://github.com/Azure/autorest) that generates TypeScript code. The transpiled javascript code is isomorphic. It can be run in browser and in node.js environment.
MIT License
179 stars 75 forks source link

Typescript API Requires Credentials by Default when Documentation Says Otherwise #1416

Closed xxmissingnoxx closed 2 years ago

xxmissingnoxx commented 2 years ago

Before filling a bug

Expected behavior According to the documentation , the default behavior for generating a Typescript client should be for --add-credentials=false, but it appears to require credentials if you fail to explicitly add that flag. The documentation says "Is called --add-credentials. Defaults to false."

The autorest command used was: autorest --input-file=regular_error.yaml --typescript --output-folder=autorest/regular_error/typescript --clear-output-folder --generate-metadata=true

OpenAPI file is:

openapi: 3.0.3
info:
  title: Your Project API
  version: 1.0.0
  description: Using name clash Error type
paths:
  /blog/api/blogs/:
    get:
      operationId: blogs_list
      description: Paginated list of blogs and relevant metadata.
      parameters:
      - name: page
        required: false
        in: query
        description: A page number within the paginated result set.
        schema:
          type: integer
      - name: size
        required: false
        in: query
        description: Number of results to return per page.
        schema:
          type: integer
      tags:
      - blogs
      security:
      - cookieAuth: []
      - basicAuth: []
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PaginatedPostList'
          description: ''
    post:
      operationId: blogs_create
      tags:
      - blogs
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Input'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Input'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Input'
        required: true
      security:
      - cookieAuth: []
      - basicAuth: []
      responses:
        '204':
          description: Success
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          description: ''
  /blog/api/schema/:
    get:
      operationId: schema_retrieve
      description: |-
        OpenApi3 schema for this API. Format can be selected via content negotiation.

        - YAML: application/vnd.oai.openapi
        - JSON: application/vnd.oai.openapi+json
      parameters:
      - in: query
        name: format
        schema:
          type: string
          enum:
          - json
          - yaml
      - in: query
        name: lang
        schema:
          type: string
          enum:
          - af
          - ar
          - ar-dz
          - ast
          - az
          - be
          - bg
          - bn
          - br
          - bs
          - ca
          - cs
          - cy
          - da
          - de
          - dsb
          - el
          - en
          - en-au
          - en-gb
          - eo
          - es
          - es-ar
          - es-co
          - es-mx
          - es-ni
          - es-ve
          - et
          - eu
          - fa
          - fi
          - fr
          - fy
          - ga
          - gd
          - gl
          - he
          - hi
          - hr
          - hsb
          - hu
          - hy
          - ia
          - id
          - ig
          - io
          - is
          - it
          - ja
          - ka
          - kab
          - kk
          - km
          - kn
          - ko
          - ky
          - lb
          - lt
          - lv
          - mk
          - ml
          - mn
          - mr
          - ms
          - my
          - nb
          - ne
          - nl
          - nn
          - os
          - pa
          - pl
          - pt
          - pt-br
          - ro
          - ru
          - sk
          - sl
          - sq
          - sr
          - sr-latn
          - sv
          - sw
          - ta
          - te
          - tg
          - th
          - tk
          - tr
          - tt
          - udm
          - uk
          - ur
          - uz
          - vi
          - zh-hans
          - zh-hant
      tags:
      - schema
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/vnd.oai.openapi:
              schema:
                type: object
                additionalProperties: {}
            application/yaml:
              schema:
                type: object
                additionalProperties: {}
            application/vnd.oai.openapi+json:
              schema:
                type: object
                additionalProperties: {}
            application/json:
              schema:
                type: object
                additionalProperties: {}
          description: ''
components:
  schemas:
    Error:
      type: object
      properties:
        code:
          type: string
          description: One of a server-defined set of error codes
        message:
          type: string
          description: A human-readable representation of the error
        target:
          type: string
          description: The target of the error
        details:
          type: array
          items:
            $ref: '#/components/schemas/Error'
          description: An array of details about specific errors that led to this
            reported error
        innererror:
          allOf:
          - $ref: '#/components/schemas/InnerError'
          description: An object containing more specific information than the current
            object about the error
      required:
      - code
      - message
    ErrorResponse:
      type: object
      properties:
        error:
          allOf:
          - $ref: '#/components/schemas/Error'
          description: The error object
      required:
      - error
    InnerError:
      type: object
      properties:
        code:
          type: string
          description: A more specific error code than was provided by the container
            error.
        innererror:
          allOf:
          - $ref: '#/components/schemas/InnerError'
          nullable: true
          description: An object containing more specific information than the current
            object about the error
      required:
      - innererror
    Input:
      type: object
      properties:
        blog_pk:
          type: integer
          minimum: 0
        description:
          type: string
          description: Enter comment about blog here.
          minLength: 2
      required:
      - blog_pk
      - description
    PaginatedPostList:
      type: object
      properties:
        count:
          type: integer
          example: 123
        next:
          type: string
          nullable: true
          format: uri
          example: http://api.example.org/accounts/?page=4
        previous:
          type: string
          nullable: true
          format: uri
          example: http://api.example.org/accounts/?page=2
        results:
          type: array
          items:
            $ref: '#/components/schemas/Post'
    Post:
      type: object
      properties:
        id:
          type: integer
          readOnly: true
        title:
          type: string
          readOnly: true
          description: Title of the post.
        post_date:
          type: string
          format: date-time
          readOnly: true
          description: Date and time at which the post was created.
        j_authors:
          type: array
          items:
            type: string
          description: Author(s) of the post.
          minItems: 1
      required:
      - id
      - j_authors
      - post_date
      - title
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
    cookieAuth:
      type: apiKey
      in: cookie
      name: sessionid
sarangan12 commented 2 years ago

Starting to work on this task. Will provide updates shortly.

sarangan12 commented 2 years ago

Ok. I have done a quick analysis. The details are:

  1. In the Typescript SDK Generator, we set the addCredentials to true by default. If it has to be set to false, it has to be explicitly set. The specific piece of code is [Can be found here]:
async function getAddCredentials(
  host: AutorestExtensionHost
): Promise<boolean> {
  const addCredentials = await host.getValue("add-credentials");

  // Only set addCredentials to false if explicitly set to false
  // otherwise default to true
  if (addCredentials === false) {
    return false;
  } else {
    return true;
  }
}
  1. On the other hand, I understand the confusion as it was mentioned incorrectly here. This needs to be fixed. I will be creating a PR shortly to fix the documentation. Will update this issue, once I create the PR.
sarangan12 commented 2 years ago

I have created a PR to fix the documentation issue - https://github.com/Azure/autorest/pull/4562. Will update this issue once the PR is approved and merged.

sarangan12 commented 2 years ago

The Documentation PR has been merged. No more action items pending on this issue. If you still face any issues, Please feel free to reopen this PR.