microsoft / typespec

https://typespec.io/
MIT License
4.39k stars 206 forks source link

Add additionalProperties decorator #4321

Open Devon-White opened 1 month ago

Devon-White commented 1 month ago

Clear and concise description of the problem

Related to discussion: https://github.com/microsoft/typespec/discussions/4316

It would be helpful to have a decorator that would allow me to specify models/Unions to not accept additionalProperties.

If the property is not set, JSON schema will default to allowing additionalProperties, which could impact proper validation.

As discussed in the linked discussion, this is visibly an issue when using tools like Monaco editor.

I would expect it to look similar to such:

@additionalProperties(false)
union Test {
  A,
  B,
  C
}

Checklist

Devon-White commented 1 month ago

The more I think about it, I actually think it would be beneficial that additionalProperties are set to false by default upon compile a TypeSpec model.

I personally don't understand why JSON schemas by default will treat a object to have additionalProperties if not defined.

Maybe i'm not seeing something, but this feels counter-intuitive for the sake a validation? I would imagine it should always be treated as false by default and you have the option to enable it.

This all said, perhaps TypeSpec should by default generate with additionalProperties set to false unless the proposed decorator is passed and set to true, or you use a spread operator?

jeremiahlee commented 13 hours ago

+1 for the need of this

additionalProperties: false is a hard requirement for OpenAPI schemas used by many financial institutions. More data than expected is a liability. I think false should be the default and the decorator should be used when wanting to set additionalProperties to true.