rametta / rapini

:leafy_green: OpenAPI to React Query (or SWR) & Axios
Apache License 2.0
152 stars 18 forks source link

$ref: ./file.yaml: can't resolve components defined in a different file #6

Closed krhubert closed 2 years ago

krhubert commented 2 years ago

Hey,

I want to use components that are defined in different file, but it's impossible to share let's say enum (or any other component). This makes $ref: files unusable (except to few top level fields like paths or componetns)

package.json

{
  "devDependencies": {
    "rapini": "^1.7.0"
  }
}

spec.yaml

openapi: 3.0.0
info:
  title: 'api'
  version: 0.0.1

paths:
  /a:
    get:
      operationId: getA
      responses:
        '200':
          description: '200'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TypeA'

components:
  schemas:
    # I want to store component type per file
    # but it's impossible to use a component defined
    # in different file
    SharedEnumType:
      type: string
      enum: ['PENDING', 'DONE']

    TypeA:
      $ref: './type_a.yaml'

    TypeB:
      type: object
      properties:
        b:
          $ref: '#/components/schemas/SharedEnumType'

type_a.yaml

type: object
properties:
  a:
    $ref: '#/components/schemas/SharedEnumType' # works with type: string or if this is include in spec.yaml directly

cmd to run:

npx rapini -p spec.yaml -o gen -b /v3

result index.js (see #1):

import type { AxiosInstance, AxiosRequestConfig } from "axios";
import { useQuery, useMutation, useQueryClient, type QueryClient, type UseMutationOptions, type UseQueryOptions, type MutationFunction, type UseMutationResult, type UseQueryResult } from "react-query";
export type SharedEnumType = "PENDING" | "DONE";
export type TypeA = ./type_a.yaml; // #1 HERE
export type TypeB = {
    b?: SharedEnumType;
};
rametta commented 2 years ago

Hi @krhubert, thanks for reporting, will try to get it to it sometime next week. If it's urgent for you, feel free to try to open a PR sooner and I'll review it

rametta commented 2 years ago

@krhubert This should be fixed and supported now, please try with rapini@1.9.0 🎉 .

One small note I noticed about your example though, the proper syntax for inside the type_a.yaml file to reference the main file should be spec.yaml#/components/schemas/SharedEnumType to my understanding