Conversation

gersongoulart

Introducing a simple, yet dynamic solution to conquer issue #1268 with unparalleled flexibility! 🚀🌟

Overriding Built-In Templates

The parameter templateOverrides allows the override of any template or partial with a custom implementation.

Example:

package.json

{
    "scripts": {
        "generate": "openapi --input ./spec.json --output ./generated --templateOverrides index:\"Hello World\" exportService:./templates/service.hbs"
    },
}

NodeJs

const OpenAPI = require('openapi-typescript-codegen');

OpenAPI.generate({
    input: './spec.json',
    output: './generated',
    templateOverrides: {
        index: 'Hello World', // the template itself
        exportService: './templates/service.hbs', // or a file path
    },
});

The parameter `templateOverrides` allows the override of any template or partial with a custom implementation.

## Example:

package.json
```json
{
    "scripts": {
        "generate": "openapi --input ./spec.json --output ./generated --templateOverrides index:\"Hello World\" service:./templates/service.hbs"
    },
}
```

```typescript
const OpenAPI = require('openapi-typescript-codegen');

OpenAPI.generate({
    input: './spec.json',
    output: './generated',
    templateOverrides: {
        index: 'templates/index.hsb',
        service: 'templates/service.hsb',
    },
});
```
},
});

return eval(`(function(){return ${templateSpec} }());`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval seems safe enough, although I suspect code quality tools will complain.

Here is an another (extremely hacky) alternative using a temp file which gets resolved

import { readFileSync, writeFileSync, mkdtempSync } from 'fs';

...

    const tempDir = mkdtempSync('template-');
    const tempFilePath = join(tempDir, 'template.js');
    writeFileSync(tempFilePath, `module.exports = ${compiled};`);

    const module = require(resolve(tempFilePath));
    execSync(`rm -rf ${tempDir}`);
    return module;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the observation. Yeah, code quality tools will warn about the safety of this to prevent developers from shipping code that could be exploited. Seeing this is a library and the provider of the template to be evaluated is the developer using the library, I think this is a safe implementation of the eval function — and the cleanest solution I could find. Writing and reading to disk on every precompilation would significantly affect performance and might encounter problems with disk access permissions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thoughts. I wish we could use some other api besides handlebars.precompile. But I could not find any that works.

import { registerHandlebarHelpers } from './registerHandlebarHelpers';

export type TemplateOverrideNames =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this type can be added to the Options type for generate api in types/index.d.ts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thank you!

…partial" schema.

- Add `templateOverrides` type to the Options type for generate api in types/index.d.ts
- Update README.md file
gersongoulart added a commit to provantagex/openapi-typescript-codegen that referenced this pull request Jan 29, 2024
@salzhrani

can we assist with getting this PR reviewed and merged?

@sudhir4ev

Any update on this? Bump!

@ng-state

any updates?

@mrlubos

@ng-state this package is no longer maintained, see README

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.