@kbn/openapi-generator: Support object keys which need to be surrounded by quotes (#172049)

## Summary

 Are there tests for @kbn/openapi-generator? I couldn't find them  

Our team had a `@timestamp` field in an API response, this caused the
zod schema to break because the key was not being escaped in the object
e.g:

```js
export const AssetCriticalityRecord =       CreateAssetCriticalityRecord.and(z.object({
      //  invalid JS
      @timestamp: z.string().datetime(),
  }));
```

With the fix, the `@timestamp` key is surrounded by quotes.

```js
export const AssetCriticalityRecord =       CreateAssetCriticalityRecord.and(z.object({
     //  
      '@timestamp': z.string().datetime(),
  }));
```

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Mark Hopkin 2023-11-29 11:43:35 +00:00 committed by GitHub
parent ad177dd94f
commit bf5f8cb480
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -47,4 +47,7 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) {
handlebarsInstance.registerHelper('isUnknown', (val: object) => {
return !('type' in val || '$ref' in val || 'anyOf' in val || 'oneOf' in val || 'allOf' in val);
});
handlebarsInstance.registerHelper('startsWithSpecialChar', (val: string) => {
return /^[^a-zA-Z0-9]/.test(val);
});
}

View file

@ -80,7 +80,11 @@ z.unknown()
* {{{description}}}
*/
{{/if}}
{{@key}}: {{> zod_schema_item requiredBool=(includes ../required @key)}},
{{#if (startsWithSpecialChar @key)}}
'{{@key}}': {{> zod_schema_item requiredBool=(includes ../required @key)}},
{{else}}
{{@key}}: {{> zod_schema_item requiredBool=(includes ../required @key)}},
{{/if}}
{{/each}}
})
{{~#if (eq additionalProperties false)}}.strict(){{/if~}}