Skip to content

Commit

Permalink
Add general generation options, starting with configuring the request…
Browse files Browse the repository at this point in the history
…Body identifier for an operation
  • Loading branch information
karlvr committed Nov 21, 2023
1 parent fa0f230 commit 29717b7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .changeset/lucky-lions-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@openapi-generator-plus/testing": minor
"@openapi-generator-plus/types": minor
"@openapi-generator-plus/core": minor
"openapi-generator-plus": minor
---

Add general generation options, starting with configuring the requestBody identifier for an operation
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,22 @@ The configuration file may be YAML or JSON. A basic configuration file contains:
|`inputPath`|`string`|The path to the input API specification, relative to the config file.|`undefined`|
|`outputPath`|`string`|The path to the output directory, relative to the config file.|`undefined`|
|`generator`|`string`|The name of the generator template, or the path relative to the config file for the generator template module.|`undefined`|
|`options`|`Options`|Additional options to control the generation.|`undefined`|

See the README for the generator template you're using for additional configuration options supported by that generator.

#### Options

|Property|Type|Description|Default|
|--------|----|-----------|-------|
|`operations`|`Operations`|Options specific to generation of operations|`undefined`|

#### Operations

|Property|Type|Description|Default|
|--------|----|-----------|-------|
|`defaultRequestBodyIdentifier`|`string`|The identifier to use for the request body (OpenAPI 3)|`"request"`|

## Background

OpenAPI Generator Plus is a reimplementation of code generation for OpenAPI specifications, following
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createMyGeneratorContext() {
async function generate(config: CommandLineConfig, generatorConstructor: CodegenGeneratorConstructor): Promise<boolean> {
const generator = constructGenerator(config, createMyGeneratorContext(), generatorConstructor)

const state = createCodegenState(generator)
const state = createCodegenState(config, generator)
state.log = log
const input = await createCodegenInput(config.inputPath)

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export async function createTestResult(inputPath: string, config?: TestCodegenCo
}

function createTestCodegenState(generator: CodegenGenerator, config?: TestCodegenConfig): CodegenState {
const state = createCodegenState(generator)
const state = createCodegenState({}, generator)
state.log = function(level, message) {
if (level >= CodegenLogLevel.WARN && (!config || !config.expectLogWarnings)) {
/* We should not emit any warning messages during the tests */
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ export function constructGenerator(config: CodegenConfig, context: CodegenGenera
return generator
}

export function createCodegenState(generator: CodegenGenerator): CodegenState {
export function createCodegenState(config: CodegenConfig, generator: CodegenGenerator): CodegenState {
return {
generator,
options: {
operations: {
defaultRequestBodyIdentifier: (config as any).options?.operations?.defaultRequestBodyIdentifier || 'request',
},
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/process/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export function toCodegenOperation(fullPath: string, method: string, operation:

const defaultContent = requestBodyContents[0]
bodyParam = {
name: toUniqueName('request', undefined, parameters, state),
serializedName: 'request', /* This doesn't actually have a serialized name as we created it */
name: toUniqueName(state.options.operations.defaultRequestBodyIdentifier, undefined, parameters, state),
serializedName: state.options.operations.defaultRequestBodyIdentifier, /* This doesn't actually have a serialized name as we created it */

required: defaultContent.required,
schema: defaultContent.schema,
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface CodegenResult {

export async function createCodegenResult(inputPath: string, config: CodegenConfig, generatorConstructor: CodegenGeneratorConstructor): Promise<CodegenResult> {
const generator = constructGenerator(config, createGeneratorContext(), generatorConstructor)
const state = createCodegenState(generator)
const state = createCodegenState(config, generator)
const input = await createCodegenInput(inputPath)
const doc = createCodegenDocument(input, state)
return {
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export interface CodegenInputDocument {
export interface CodegenState {
generator: CodegenGenerator
log?: CodegenLogFunction
options: CodegenOptions
}

export interface CodegenOptions {
operations: {
defaultRequestBodyIdentifier: string
}
}

export type CodegenLogFunction = (level: CodegenLogLevel, message: string) => void
Expand Down

0 comments on commit 29717b7

Please sign in to comment.