openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

`$ref` with other keywords doesn't work

Open zyoshoka opened this issue 1 year ago • 2 comments

Description

In the specifications up to the JSON Schema Draft 7, if you included other keywords in an object using $ref, they were ignored^1, but starting with the Draft 2019-09, you are allowed to include other properties in $ref^2. Since the OAS 3.1.0 is 100% compatible with the Draft 2020-12^3, 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

zyoshoka avatar Mar 10 '24 03:03 zyoshoka

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 avatar Mar 12 '24 20:03 drwpow

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"] }
                ]
            }
        }
    }
}

armaneous avatar Apr 11 '24 04:04 armaneous

This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

github-actions[bot] avatar Aug 06 '24 12:08 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

github-actions[bot] avatar Aug 14 '24 02:08 github-actions[bot]