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

fix(angular): avoid Angular type error when nullable #1200

Merged

Conversation

soartec-lab
Copy link
Collaborator

@soartec-lab soartec-lab commented Feb 4, 2024

Status

READY

Description

If we specify nullable: true in OpenAPI, | null" is added to the parameter type, but httpParams type in Angular doesn't support the null type.

As a result, the following type error will occur.

Type 'number | null' is not assignable to type 'string | number | boolean | readonly (string | number | boolean)[]'.

See also angular/angular#20564

So I fixed it so that it doesn't add the nullable type when the client is angular.

Related PRs

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

Outline the steps to test or reproduce the PR here.

  1. Specify nullable: true for the parameter
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          schema:
            type: integer
            nullable: true
            maximum: 100
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pets'
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      maxItems: 100
      items:
        $ref: '#/components/schemas/Pet'
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
  1. specify angular to client
module.exports = {
  'petstore-file': {
    input: {
      target: './petstore.yaml',
    },
    output: {
      mode: 'tags-split',
      client: 'angular',
      target: 'src/gen/endpoints',
      schemas: 'src/gen/model',
    },
  },
};

3 execute orval

orval
  1. check the generated code

Before

export type ListPetsParams = {
  /**
   * How many items to return at one time (max 100)
   */
  limit?: number | null;
};

After

export type ListPetsParams = {
  /**
   * How many items to return at one time (max 100)
   */
  limit?: number;
};

@soartec-lab soartec-lab added the bug Something isn't working label Feb 4, 2024
@soartec-lab soartec-lab added this to the 6.25.0 milestone Feb 4, 2024
Copy link
Collaborator

@melloware melloware left a comment

Choose a reason for hiding this comment

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

Ahh I read the ticket. Too bad angular has this issue to make us work around it but I understand.

@melloware melloware merged commit cd0a56e into anymaniax:master Feb 4, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants