karlvr/openapi-generator-plus

How do I create separate files for each group?

Closed this issue · 6 comments

I want to create a separate file for each group in my Open API spec. Is this possible?

@ktalebian This would depend upon the generator implementation that you're using... they all behave a little differently. I'm guessing that you're using a generator outputting TypeScript? It creates quite a monolithic file.

I would be very happy for it to output multiple files. Am I right about what you're trying to do? Are you in a position to look at the code and perhaps create a PR?

@karlvr, I am more than happy to contribute. Can you point me to where I should be doing this?

Yes, you are right. I'm creating my own template for the library I'm using and I want to use this to generate the routes and controllers for the typescript application.

@ktalebian cool. In which case if you're making your own generator template then who choose how to create files from the parsed spec.

If you look at my TypeScript generator it emits files in its myContext.additionalExportTemplates method, which is called by the exportTemplates method in the TypeScript common generator.

Each of those templates is Handlebars and is supplied with a specific root context, e.g. { ...rootContext, ...doc }, so if you want to output a different template for each API group take a look at the Java generator in its myContext.additionalExportTemplates... this is what that code looks like:

		for (const group of doc.groups) {
			const operations = group.operations
			if (!operations.length) {
				continue
			}
			await emit('api', path.join(outputPath, relativeSourceOutputPath, apiPackagePath, `${context.generator().toClassName(group.name)}Api.java`), 
				{ ...rootContext, ...group, operations }, true, hbs)
		}

It looks at the API groups in the doc and then runs the 'api' template for each group and outputs it to a file named for the group, ${context.generator().toClassName(group.name)}Api.java.

The generators are a bit of a tangle of inheritance... I would really like to untangle them somewhat but I haven't managed it. Hopefully this gives you enough leads to proceed.

@karlvr makes sense. Thanks.

One question - all of the nativeTypes get a prefix of Api. - is there a way to remove that? This Api. implies everything is being created in a single file with a namespace Api. If I'm going to have individual files, these instead need to be imported.

I'm right now manually iterating over the entire object, and replacing the Api. with nothing. Is there a better way of doing this?

@ktalebian I think you're talking about a specific generator; the TypeScript generator? If you're creating your own generator template then you can simply not do that.

Check out https://github.com/karlvr/openapi-generator-plus-generators/blob/4415128891549613d0309b716a1d2dc52a9f21a9/packages/typescript-common/src/index.ts#L384. If you are extending from this generator then you can override this function to change the behaviour.

The core module is responsible for parsing the spec and building the object model that is useful for generation, and part of that project is using functions like this one that are supplied by the generator. Finally the generator is used to actually emit the files.

Ah perfect thanks!