Skip to content

fix: Fix schema generation for oneOf when using TS-FETCH client#18154

Merged
macjohnny merged 2 commits intoOpenAPITools:masterfrom
giovannicimolin:giovanni/fix-ts-fetch-pesky-bug
Mar 19, 2024
Merged

fix: Fix schema generation for oneOf when using TS-FETCH client#18154
macjohnny merged 2 commits intoOpenAPITools:masterfrom
giovannicimolin:giovanni/fix-ts-fetch-pesky-bug

Conversation

@giovannicimolin
Copy link
Contributor

@giovannicimolin giovannicimolin commented Mar 18, 2024

I'm using Django REST Framework + DRF Spectacular to automatically generate OpenAPI 3 schemas for my APIs.
When defining nullable enums, the API generator typescript-fetch creates code that doesn't compile properly with two errors:

  • Spread types may only be created from object types (on FromJSONTyped functions).
  • Module '"./BlankEnum"' has no exported member 'instanceOfBlankEnum' (enum templaces don't export function used by other API client files).

There's a problem generating typescript-fetch API clients using schemas containing oneOf and Enums.
Some parts of the API client seem to rely on the instaceOf{{classname}}, not provided for enum models.
Also, the code tries to create spread types from function types, leading to a TS compilation error.

I'm not sure this is the best way to fix the issue, my OpenAPI generator internals knowledge is somewhat limited.

Testing instructions

  1. Check out this branch.
  2. Build the generator.
  3. Generate an API client using the following schema:
openapi: 3.0.3
info:
  title: Some API
  version: 1.0.0
  description: Some API documentation (internal).
paths:
  /api/v1/some-api/{id}/:
    patch:
      operationId: some_api_partial_update
      description: A viewset to edit some api information
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        required: true
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SomeRequest'
      security:
      - TokenAuthentication: []
      - basicAuth: []
      - cookieAuth: []
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/SomeRequest'
          description: ''
components:
  schemas:
    SomeRequest:
      type: object
      description: Some item serializer.
      properties:
        item_type:
          nullable: true
          oneOf:
          - $ref: '#/components/schemas/ItemTypeEnum'
          - $ref: '#/components/schemas/BlankEnum'
          - $ref: '#/components/schemas/NullEnum'
    BlankEnum:
      enum:
      - ''
    ItemTypeEnum:
      enum:
      - A
      - B
      - C
      type: string
    NullEnum:
      enum:
      - null
  1. Create new node env, install typescript and run tsc (target: es2017).
  2. Check that no type errors are reported by Typescript.

Related issues

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

This fixes the following errors:
* Spread types may only be created from object types (on `FromJSONTyped` functions).
* Module '"./BlankEnum"' has no exported member 'instanceOfBlankEnum' (enum templaces don't export function used by other API client files).
@giovannicimolin
Copy link
Contributor Author

giovannicimolin commented Mar 18, 2024

Pinging the members of the Typescript technical committee:

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

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

LGTM

thanks for your contribution

@macjohnny macjohnny merged commit 93b76dd into OpenAPITools:master Mar 19, 2024
@giovannicimolin giovannicimolin deleted the giovanni/fix-ts-fetch-pesky-bug branch March 19, 2024 19:31
zapodot pushed a commit to zapodot/openapi-generator that referenced this pull request Mar 21, 2024
…enAPITools#18154)

* fix: Fix schema generation for `oneOf` when using TS-FETCH client

This fixes the following errors:
* Spread types may only be created from object types (on `FromJSONTyped` functions).
* Module '"./BlankEnum"' has no exported member 'instanceOfBlankEnum' (enum templaces don't export function used by other API client files).

* chore: Regenerate samples
@wing328 wing328 added this to the 7.5.0 milestone Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants