Skip to content

Generation of schema that references outside schema #1131

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

Closed
1 task done
oondaasta opened this issue May 23, 2023 · 2 comments · Fixed by #1212
Closed
1 task done

Generation of schema that references outside schema #1131

oondaasta opened this issue May 23, 2023 · 2 comments · Fixed by #1212
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@oondaasta
Copy link

Description

Generation of a schema, that references other schemas in the $ref results in a invalid schema.ts, with "unkown" types and wrong property referencing Property 'Problem' does not exist on type 'unknown'

Name Version
openapi-typescript 6.2.4
Node.js 20.1.0
OS + version macOS 13, Windows 11, etc.

Reproduction

Run following code as a .yml in the generator:

openapi: 3.0.0
servers:
  - url: https://dummy.to.server/v1
info:
  description: Test YAML
  version: 0.1.0
  title: Test
  termsOfService: ''
security:
  - bearerAuth: [ ]
tags:
  - name: Test Object
    description: Basic operations on a test object
paths:
  /test-object/{uuid}:
    get:
      summary: Returns a test object
      tags:
        - Test Object
      description: Returns a single test object if exists
      parameters:
        - name: uuid
          in: path
          required: true
          schema:
            type: string
      operationId: getLatestTOByUUID
      responses:
        '200':
          description: Test Object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestObject'
        '404':
          description: Test Object not found
          content:
            application/problem+json:
              schema:
                $ref: 'https://opensource.zalando.com/restful-api-guidelines/models/problem-1.0.0.yaml#/Problem'
        default:
          description: Unexpected error
          content:
            application/problem+json:
              schema:
                $ref: 'https://opensource.zalando.com/restful-api-guidelines/models/problem-1.0.0.yaml#/Problem'
components:
  schemas:
    TestObject:
      type: object
      properties:
        uuid:
          type: string
        id:
          type: integer
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

How can this be reproduced / when did the error occur?

Generate a schema based on the prior .yml file.

Expected result

(in case it’s not obvious)

Checklist

@oondaasta oondaasta added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels May 23, 2023
@drwpow
Copy link
Contributor

drwpow commented May 23, 2023

This seems like a definite bug. Thank you for the thorough reproduction! 🙏 That helps immensely in diagnosing the problem.

@mitchell-merry
Copy link
Contributor

mitchell-merry commented May 28, 2023

The issue here is that the external schema looks like:

Problem:
  type: object
  properties:
    type:
      type: string
      format: uri
      description: |
        An absolute URI that identifies the problem type.  When dereferenced,
        it SHOULD provide human-readable documentation for the problem type
        (e.g., using HTML).
      default: 'about:blank'
      example: 'https://zalando.github.io/problem/constraint-violation'
    title:
      type: string
      description: |
        A short, summary of the problem type. Written in english and readable
        for engineers (usually not suited for non technical stakeholders and
        not localized); example: Service Unavailable
    status:
      type: integer
      format: int32
      description: |
        The HTTP status code generated by the origin server for this occurrence
        of the problem.
      minimum: 100
      maximum: 600
      exclusiveMaximum: true
      example: 503
    detail:
      type: string
      description: |
        A human readable explanation specific to this occurrence of the
        problem.
      example: Connection to database timed out
    instance:
      type: string
      format: uri
      description: |
        An absolute URI that identifies the specific occurrence of the problem.
        It may or may not yield further information if dereferenced.

Even though the $ref specifies Problem in the example:

$ref: 'https://opensource.zalando.com/restful-api-guidelines/models/problem-1.0.0.yaml#/Problem'

the subschema is being read as

{
  "hint": "SchemaObject",
  "schema": {
    "Problem": {
      "type": "object",
      "properties": { ... }
    }
  }
}

instead of

{
  "hint": "SchemaObject",
  "schema": {
    "type": "object",
    "properties": { ... }
  }
}

(So in this case, not a SchemaObject, but one level above, the Map<string, SchemaObject>)

@drwpow drwpow mentioned this issue Jul 7, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants