-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
aws-apigateway: JsonSchema expects type as enum type #20055
Comments
What's wrong with our current JsonSchemaType enum? This works perfectly fine so there's no need to deprecate it const schema: JsonSchema = {
type: JsonSchemaType.OBJECT,
properties: {
name: {
type: JsonSchemaType.STRING
}
},
required: ['name']
}
api.addModel('event', {
modelName: 'aanalytivcevent',
contentType: 'application/json',
schema: schema
}) |
This assumes that you create the JSON schema solely for AWS. But that's not what JSON schema is for - it's a universal format usable across different other technologies, e.g. I'm also using the same schema to run validations inside the lambda (specifically to differentiate between multiple different inputs that would all be valid). The code which creates the JSON schema isn't even aware of AWS, so importing something from AWS in there would force me to make a application module dependent on AWS CDK. This problem also shows up when importing a json file directly: TypeScript can detect its structure, but will complain that the string The other way around, I don't know what would be lost by expecting a const string instead of an Enum, so it would make use cases either without losing anything (unless there is some jsii magic I'm not aware of). |
This all makes sense @dbartholomae, thanks for the explanation. Forcing users to use our enum when it would otherwise be easily portable sounds less than ideal. I disagree with deprecating the enum - but I think we could make it so that the properties accept the enum or a string. We can easily add checks to make sure the string inputs are valid as well |
Thanks! Deprecating the enum might not be worth it, I agree. It would have the following benefits: I'm fine with a decision in either direction and could then also create a PR. I would recommend, though, to then also reopen the following issue: |
Moving the discusison from the PR into this issue:
Ideally, this would use a meaningful input type (basically the existing JsonSchema type, but with unions and ReadOnlyArrays), and, ideally be in a meaningful place. A really nice interface for this would be if Do you have an example for me for what is the preferred way for these kind of utils in this repository? Then I could make an alternate suggestion. The code for this than would be straightforward:
|
I agree with @dbartholomae that this would be really useful. My use case is deriving the API Gateway model validation schema from a TypeScript type:
Ideally, I could reuse the
This would mean that I do not have to keep the event body type and the validation model in sync manually. Currently this does not work because typescript-json-schema uses type strings like |
Currently, the PR is stuck as I'm waiting on input on the solution design. @estrehle one workaround for now is to just |
Describe the bug
When hand-writing a schema or importing a schema from somewhere else to use in multiple places, including apigateway, TypeScript rejects the schema:
Expected Behavior
This should just work
Current Behavior
Reproduction Steps
If the example above is not enough to show the issue, I can create a reproducing repo.
Possible Solution
Introduce a string union type for
JsonSchemaTypeString
and use it here:https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-apigateway/lib/json-schema.ts#L34
Keep the exported
JsonSchemaType
enum to avoid a breaking change and deprecate it.Additional Information/Context
No response
CDK CLI Version
2.20.0
Framework Version
No response
Node.js Version
16.13.1
OS
Windows 11
Language
Typescript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: