Skip to content

Commit b3749af

Browse files
committed
fix: process object index signature jsdoc
1 parent bfca775 commit b3749af

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

docs/modules/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
- [OpenApiDocumentPatchPathItem](index.md#openapidocumentpatchpathitem)
2525
- [OpenApiDocumentPatchSchema](index.md#openapidocumentpatchschema)
2626
- [OpenApiDocumentPatchTags](index.md#openapidocumentpatchtags)
27+
- [OpenApiSchemaFieldPathItem](index.md#openapischemafieldpathitem)
28+
29+
### Variables
30+
31+
- [stringIndexSignature](index.md#stringindexsignature)
2732

2833
## Type Aliases
2934

@@ -174,3 +179,17 @@ Callback to patch tags.
174179
##### Returns
175180

176181
[`OpenApiTag`](../interfaces/openapi.OpenApiTag.md)[] \| `Promise`\<[`OpenApiTag`](../interfaces/openapi.OpenApiTag.md)[]\>
182+
183+
___
184+
185+
### OpenApiSchemaFieldPathItem
186+
187+
Ƭ **OpenApiSchemaFieldPathItem**: `string` \| typeof [`stringIndexSignature`](index.md#stringindexsignature)
188+
189+
Path item used to reference a path to a field in the schema.
190+
191+
## Variables
192+
193+
### stringIndexSignature
194+
195+
`Const` **stringIndexSignature**: typeof [`stringIndexSignature`](index.md#stringindexsignature)

docs/modules/openapi_client.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ ___
213213

214214
### GenerateModelJsDoc
215215

216-
Ƭ **GenerateModelJsDoc**: (`params`: \{ `fieldPath`: `string`[] ; `schema`: [`OpenApiSchema`](openapi.md#openapischema) ; `schemaName`: `string` ; `suggestedJsDoc`: [`JsDocBlock`](../interfaces/index.JsDocBlock.md) }) => [`JsDocBlock`](../interfaces/index.JsDocBlock.md)
216+
Ƭ **GenerateModelJsDoc**: (`params`: \{ `fieldPath`: [`OpenApiSchemaFieldPathItem`](index.md#openapischemafieldpathitem)[] ; `schema`: [`OpenApiSchema`](openapi.md#openapischema) ; `schemaName`: `string` ; `suggestedJsDoc`: [`JsDocBlock`](../interfaces/index.JsDocBlock.md) }) => [`JsDocBlock`](../interfaces/index.JsDocBlock.md)
217217

218218
#### Type declaration
219219

@@ -226,7 +226,7 @@ Callback for generating the JSDoc of a model.
226226
| Name | Type | Description |
227227
| :------ | :------ | :------ |
228228
| `params` | `Object` | - |
229-
| `params.fieldPath` | `string`[] | Path to the field in the schema. Empty if the schema is a model schema. |
229+
| `params.fieldPath` | [`OpenApiSchemaFieldPathItem`](index.md#openapischemafieldpathitem)[] | Path to the field in the schema. Empty if the schema is a model schema. |
230230
| `params.schema` | [`OpenApiSchema`](openapi.md#openapischema) | OpenAPISchema of the model schema (if fieldPath is empty) or the field schema. |
231231
| `params.schemaName` | `string` | Name of the schema. |
232232
| `params.suggestedJsDoc` | [`JsDocBlock`](../interfaces/index.JsDocBlock.md) | Suggested JSDoc block. Used by default if the callback is not specified. |

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export type {
1313
} from './schema-to-typescript/config';
1414
export type {JsDocBlockTag, JsDocBlock} from './utils/jsdoc';
1515
export type {FilenameFormat, EntityNameCase} from './utils/string-utils';
16+
export {stringIndexSignature, type OpenApiSchemaFieldPathItem} from './schema-to-typescript/common';

src/schema-to-typescript/common.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ import {attachJsDocComment, extractJsDoc, JsDocBlock, JsDocRenderConfig, renderJ
5151
import {applyEntityNameCase} from '../utils/string-utils';
5252
import {simplifyIntersectionTypeIfPossible, simplifyUnionTypeIfPossible} from '../utils/type-utils';
5353

54+
export const stringIndexSignature = Symbol('stringIndexSignature');
55+
56+
/**
57+
* Path item used to reference a path to a field in the schema.
58+
*/
59+
export type OpenApiSchemaFieldPathItem = string | typeof stringIndexSignature;
60+
5461
export interface GenerateSchemaTypeParams {
5562
schema: OpenApiSchema;
5663
getTypeName: (name: string, schema: OpenApiSchema) => string;
5764
getBinaryType: () => TSType;
5865
expand?: boolean;
59-
processJsDoc?: (jsdoc: JsDocBlock, entity: OpenApiSchema, path: string[]) => JsDocBlock;
60-
processJsDocPath?: string[];
66+
processJsDoc?: (jsdoc: JsDocBlock, entity: OpenApiSchema, path: OpenApiSchemaFieldPathItem[]) => JsDocBlock;
67+
processJsDocPath?: OpenApiSchemaFieldPathItem[];
6168
jsDocRenderConfig?: JsDocRenderConfig;
6269
}
6370

@@ -234,16 +241,25 @@ export function generateSchemaType({
234241
if (typeof additionalProperties === 'object' && additionalProperties.title) {
235242
keyName = applyEntityNameCase(additionalProperties.title, 'camelCase');
236243
}
244+
let jsdoc = extractJsDoc(additionalProperties);
245+
const currentProcessJsDocPath = (processJsDocPath ?? []).concat(stringIndexSignature);
246+
if (processJsDoc) {
247+
jsdoc = processJsDoc(jsdoc, additionalProperties, currentProcessJsDocPath);
248+
}
237249
objectIntersection.push(
238250
tsTypeLiteral([
239251
attachJsDocComment(
240252
tsIndexSignature(
241253
[attachTypeAnnotation(identifier(keyName), tsTypeAnnotation(tsStringKeyword()))],
242254
tsTypeAnnotation(
243-
generateSchemaType({schema: additionalProperties, ...commonSchemaGenerationOptions})
255+
generateSchemaType({
256+
schema: additionalProperties,
257+
...commonSchemaGenerationOptions,
258+
processJsDocPath: currentProcessJsDocPath
259+
})
244260
)
245261
),
246-
renderJsDoc(extractJsDoc(additionalProperties), jsDocRenderConfig)
262+
renderJsDoc(jsdoc, jsDocRenderConfig)
247263
)
248264
])
249265
);

src/schema-to-typescript/common/models.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ import {attachJsDocComment, extractJsDoc, JsDocRenderConfig, renderJsDoc} from '
3939
import {getRelativeImportPath} from '../../utils/paths';
4040
import {applyEntityNameCase, formatFilename} from '../../utils/string-utils';
4141
import {ExtractedTags} from '../../utils/tags';
42-
import {attachTypeAnnotation, CommentsRenderConfig, generateSchemaType, renderTypeScript} from '../common';
42+
import {
43+
attachTypeAnnotation,
44+
CommentsRenderConfig,
45+
generateSchemaType,
46+
OpenApiSchemaFieldPathItem,
47+
renderTypeScript
48+
} from '../common';
4349
import {ClientGenerationResultFile} from '../config';
4450
import {
4551
GenerateModelJsDoc,
@@ -89,7 +95,7 @@ function generateTypeExport({
8995
extendDependenciesAndGetResult(generateBinaryType(binaryTypes, importPath), dependencyImports),
9096
processJsDoc:
9197
generateJsDoc &&
92-
((jsdoc, schema, path: string[]) =>
98+
((jsdoc, schema, path: OpenApiSchemaFieldPathItem[]) =>
9399
generateJsDoc({
94100
suggestedJsDoc: jsdoc,
95101
schema,

src/schema-to-typescript/openapi-to-typescript-client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {OpenApiSchemaFieldPathItem} from './common';
12
import {generateClient} from './common/client';
23
import {
34
generateCommonHttpClient,
@@ -462,7 +463,7 @@ export type GenerateModelJsDoc = (params: {
462463
/**
463464
* Path to the field in the schema. Empty if the schema is a model schema.
464465
*/
465-
fieldPath: string[];
466+
fieldPath: OpenApiSchemaFieldPathItem[];
466467
}) => JsDocBlock;
467468

468469
/**

0 commit comments

Comments
 (0)