Skip to content

Commit

Permalink
@kbn/openapi-generator: Support object keys which need to be surround…
Browse files Browse the repository at this point in the history
…ed 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>
  • Loading branch information
hop-dev and kibanamachine committed Nov 29, 2023
1 parent ad177dd commit bf5f8cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -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);
});
}
Expand Up @@ -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~}}
Expand Down

0 comments on commit bf5f8cb

Please sign in to comment.