Skip to content

Commit db4b325

Browse files
committed
Fix examples and resolve local references correctly
1 parent 329a079 commit db4b325

File tree

7 files changed

+34
-12
lines changed

7 files changed

+34
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ npm install -g openapi3-generator
2828
-V, --version output the version number
2929
-o, --output <outputDir> directory where to put the generated files (defaults to current directory)
3030
-t, --templates <templateDir> directory where templates are located (defaults to internal templates directory)
31+
-b, --basedir <baseDir> directory to use as the base when resolving local file references (defaults to OpenAPI file directory)
3132
-h, --help output usage information
3233
```
3334

cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const green = text => `\x1b[32m${text}\x1b[0m`;
1313

1414
let openapiFile;
1515
let template;
16+
let baseDir;
1617

1718
const parseOutput = dir => path.resolve(dir);
1819

@@ -22,6 +23,7 @@ program
2223
.action((openapiFilePath, tmpl) => {
2324
if (!openapiFilePath.startsWith('http:') && !openapiFilePath.startsWith('https:')) {
2425
openapiFile = path.resolve(openapiFilePath);
26+
baseDir = path.dirname(openapiFile);
2527
} else {
2628
openapiFile = openapiFilePath;
2729
}
@@ -30,6 +32,7 @@ program
3032
})
3133
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
3234
.option('-t, --templates <templateDir>', 'directory where templates are located (defaults to internal nodejs templates)')
35+
.option('-b, --basedir <baseDir>', 'directory to use as the base when resolving local file references (defaults to OpenAPI file directory)')
3336
.parse(process.argv);
3437

3538
if (!openapiFile) {
@@ -39,6 +42,7 @@ if (!openapiFile) {
3942

4043
generator.generate({
4144
openapi: openapiFile,
45+
base_dir: program.basedir || baseDir || process.cwd(),
4246
target_dir: program.output,
4347
templates: program.templates ? path.resolve(process.cwd(), program.templates) : undefined,
4448
template,

lib/bundler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ function parseContent (content) {
5555
}
5656
}
5757

58-
async function dereference (json) {
59-
return RefParser.dereference(json, {
58+
async function dereference (json, baseDir) {
59+
return RefParser.dereference(`${baseDir}/`, json, {
6060
dereference: {
6161
circular: 'ignore'
6262
}
@@ -71,7 +71,7 @@ async function bundle (json) {
7171
});
7272
}
7373

74-
async function bundler (filePath) {
74+
async function bundler (filePath, baseDir) {
7575
let content, parsedContent, dereferencedJSON, bundledJSON;
7676

7777
try {
@@ -91,7 +91,7 @@ async function bundler (filePath) {
9191
}
9292

9393
try {
94-
dereferencedJSON = await dereference(parsedContent);
94+
dereferencedJSON = await dereference(parsedContent, baseDir);
9595
} catch (e) {
9696
console.error('Can not dereference the JSON obtained from the content of the Swagger specification file');
9797
console.error(e);

lib/generator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ const registerPartials = config => new Promise((resolve, reject) => {
256256
});
257257
});
258258

259-
const bundle = async (openapi) => {
259+
const bundle = async (openapi, baseDir) => {
260260
if (typeof openapi === 'string') {
261261
try {
262-
return await bundler(openapi);
262+
return await bundler(openapi, baseDir);
263263
} catch (e) {
264264
throw e;
265265
}
@@ -278,7 +278,7 @@ const bundle = async (openapi) => {
278278
* @return {Promise}
279279
*/
280280
codegen.generate = config => new Promise((resolve, reject) => {
281-
bundle(config.openapi)
281+
bundle(config.openapi, config.base_dir)
282282
.catch(reject)
283283
.then((openapi) => {
284284
openapi = beautifier(openapi);

templates/markdown/.partials/example.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@
1717
```
1818
{{/if}}
1919
{{/if}}
20+
21+
{{#if externalValue}}
22+
{{#unless example}}
23+
##### Example
24+
{{/unless}}
25+
[Download example]({{externalValue}})
26+
{{/if}}

templates/markdown/.partials/operation.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
###### {{contentTypeName}}
1515
{{> schema schema=schema schemaName='body' hideTitle=true hideExamples=true}}
1616

17-
{{> example example=schema.example generatedExample=schema.generatedExample }}
1817
{{#if examples}}
1918
{{#each examples as |example exampleName|}}
20-
{{> example example=example.value description=example.description}}
19+
{{> example example=example.value description=example.description externalValue=example.externalValue}}
2120
{{/each}}
21+
{{else}}
22+
{{#if example}}
23+
{{> example example=example.value description=example.description externalValue=example.externalValue}}
24+
{{else}}
25+
{{> example example=schema.example generatedExample=schema.generatedExample }}
26+
{{/if}}
2227
{{/if}}
2328

2429
{{/each}}
@@ -29,11 +34,16 @@
2934
#### Request body
3035
{{> schema schema=../schema schemaName='body' hideTitle=true hideExamples=true}}
3136

32-
{{> example example=schema.example generatedExample=schema.generatedExample }}
3337
{{#if ../examples}}
3438
{{#each ../examples as |example exampleName|}}
3539
{{> example example=example.value description=example.description}}
3640
{{/each}}
41+
{{else}}
42+
{{#if example}}
43+
{{> example example=example.value description=example.description }}
44+
{{else}}
45+
{{> example example=schema.example generatedExample=schema.generatedExample }}
46+
{{/if}}
3747
{{/if}}
3848

3949
{{/equal}}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{{#if openapi.components.schemas}}
1+
{{#unless openapi.__noSchemas}}
22
## Schemas
33

44
{{#each openapi.components.schemas}}
55
{{~>schema schema=. schemaName=@key renderSlug=true~}}
66
{{/each}}
7-
{{/if}}
7+
{{/unless}}

0 commit comments

Comments
 (0)