-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[apigateway] Support loading API gateway Model schemas from file #10818
Comments
Could you provide some samples on how these models are stored in your filesystem? How complex or big is your most most complex/biggest schema? |
Simplified package outline is something like:
Our schemas are quite simple. Request models typically consist of up to 4 string parameters with validation (no significant deep-object nesting). |
If anyone finds this of use until an official method is created. I use it only for specific set of schemas as a quick and dirty way - so it comes with a wanring! import { JsonSchema, JsonSchemaVersion } from '@aws-cdk/aws-apigateway';
export const generateJSONSchemaFromObject = (input: any): JsonSchema => {
const formattedSchema: any = {};
for (const [key, value] of Object.entries(input)) {
const formattedKey = keyFormatter(key);
formattedSchema[formattedKey] = valueSwitcher(formattedKey, value);
}
return formattedSchema as JsonSchema;
};
const keyFormatter = (key: string): string => {
if (key.startsWith('$')) {
return key.slice(1);
}
return key;
};
const valueSwitcher = (key: string, value: any): any => {
if (key === 'schema') {
return schema(value as string);
}
switch (true) {
case Array.isArray(value): {
const newValues: any[] = [];
for (const val of value) {
newValues.push(valueSwitcher(key, val));
}
return newValues;
}
case typeof value === 'object':
return generateJSONSchemaFromObject(value);
default:
return value;
}
};
const schema = (value: string): JsonSchemaVersion => {
if (value !== JsonSchemaVersion.DRAFT4) {
throw new Error('api gateway model schema must be draft-04')
}
return JsonSchemaVersion.DRAFT4;
}; Updated to reflect |
+1 |
1 similar comment
+1 |
This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue. |
This is essentially the same issue as #20055, which asks for support for a generic object (without the const analyticsEventInputSchema = {
type: 'object',
properties: {
name: { type: 'string' }
}
}
const analyticsEventModel = api.addModel("AnalyticsEvent", {
modelName: "AnalyticsEvent",
contentType: "application/json",
// Not sure what the API would look like, exactly.
// But some sort of transform function would be involved here.
schema: JsonSchemas.fromObject(analyticsEventInputSchema),
}); then, loading the spec from a file is something that can be done without special CDK support: const schema = JsonSchemas.fromObject(
JSON.parse(fs.readFileSync(fileName, { encoding: 'utf-8' }))
); Closing as duplicate. |
|
API Gateway model schemas are currently specified using a JsonSchema type, which means writing the schema inline in your source code. I would like to be able to define API gateway models with schemas read from the local filesystem (preferably with syntax validation during synthesis).
Use Case
My project has API gateway models stored in files in the source directory which we want in a separate part of the source tree.
Proposed Solution
Could be implemented specifically for the API Gateway module, but it might make more sense to add an arbitrary
JsonSchema.fromFile
similar to what we have for assets.Other
Current workaround is to add an empty schema when creating the model, read the JSON schema from file and override the Cfn
Schema
property. The major drawback of this approach is that schema changes alone no longer trigger redeployments automatically, as they aren't included in the hash whenlatestDeployment
is deciding whether a redeployment is required.This is a 🚀 Feature Request
The text was updated successfully, but these errors were encountered: