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

$ref with other keywords doesn't work #1591

Open
1 of 2 tasks
zyoshoka opened this issue Mar 10, 2024 · 2 comments
Open
1 of 2 tasks

$ref with other keywords doesn't work #1591

zyoshoka opened this issue Mar 10, 2024 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library

Comments

@zyoshoka
Copy link

Description

In the specifications up to the JSON Schema Draft 7, if you included other keywords in an object using $ref, they were ignored1, but starting with the Draft 2019-09, you are allowed to include other properties in $ref2. Since the OAS 3.1.0 is 100% compatible with the Draft 2020-123, the OAS 3.1.0 allows such usage, but openapi-typescript does not reflect it in the type definition.

Name Version
openapi-typescript 7.0.0-next.8
Node.js 21.7.1
OS + version macOS 14.4

Reproduction

  1. Generate type definitions using the following document:
OpenAPI document
{
  "openapi": "3.1.0",
  "info": {
    "title": "Example API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://example.com/api"
    }
  ],
  "security": [],
  "paths": {
    "/example": {
      "get": {
        "operationId": "getExample",
        "summary": "Get example",
        "responses": {
          "200": {
            "description": "Get successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/example",
                  "properties": {
                    "b": "number"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "example": {
        "type": "object",
        "properties": {
          "a": {
            "type": "string"
          }
        },
        "required": [
          "a"
        ]
      }
    }
  }
}
  1. Then you get the following result:
content: {
  "application/json": components["schemas"]["example"];
};

Expected result

I think this would have to be:

content: {
  "application/json": components["schemas"]["example"] & { b?: number };
};

Checklist

Footnotes

  1. https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-8.3

  2. https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-02#section-8.2.4.1

  3. https://www.openapis.org/blog/2021/02/18/openapi-specification-3-1-released

@zyoshoka zyoshoka added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Mar 10, 2024
@drwpow
Copy link
Owner

drwpow commented Mar 12, 2024

This is a good addition! I think 6.x or 7.x (beta) could support this addition without it being a breaking change. Would love a PR for this if anyone is able to provide one!

@drwpow drwpow added help wanted Extra attention is needed good first issue Good for newcomers labels Mar 12, 2024
@armaneous
Copy link

Not sure if I should create another issue for this, but I think it might be related to the above. In the case of composition, required gets ignored in this scenario:

{
    ...
    "components": {
        "schemas": {
            "Foo": {
                "type": "object"
                "properties": {
                    "id": {
                        ...
                    }
                }
            },
            "Bar": {
                "allOf": [
                    { "$ref": "#/components/schemas/Foo" },
                    { "required": ["id"] }
                ]
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants