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

How could we express a model with a base model and with additional properties? #71

Closed
ArcturusZhang opened this issue Jan 2, 2024 · 1 comment

Comments

@ArcturusZhang
Copy link
Member

ArcturusZhang commented Jan 2, 2024

In our current practice, additional properties on a model is expressed as

model Foo extends Record<unknown> {}

or

model Foo is Record<unknown> {}

where the Record<> takes the position of base model.

What if the model Foo needs a base model?

We have a similar case in swagger: https://github.com/Azure/azure-rest-api-specs/blob/64260c287d69860340a7c932f1450933dbb2c886/specification/datafactory/resource-manager/Microsoft.DataFactory/stable/2018-06-01/datafactory.json#L6381
This might be an issue when we try to migrate this from swagger to typespec.

And as a superset of swagger, I believe typespec should have the ability to define a model with both base model and additional properties.

BTW this will compile:

model Foo {}

model Bar extends Foo {
  name: string;

  ...Record<unknown>
}

The corresponding openAPI2.0 result from this tsp looks like this:

  "definitions": {
    "Bar": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ],
      "allOf": [
        {
          "$ref": "#/definitions/Foo"
        }
      ]
    },
    "Foo": {
      "type": "object"
    }
  },

If I remove the line ...Record<unknown>, it does not change.

The result does not really have anything for the spreaded Record, which might be a bug: we should either support this fully or forbid it instead of ignoring it.
And if making a decorator is not an option, I believe making the above tsp working as expected might be the solution of this issue to enable a model to both have base model and additional properties.

@timotheeguerin
Copy link
Member

Design issue here microsoft/typespec#2442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants