Skip to content

Commit 7688617

Browse files
committed
fix: added readme, updated schema, refactored record.mustace
1 parent 9e79889 commit 7688617

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

types/codegen/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Code Generation
2+
3+
This directory contains OpenAPI code generation configuration and post-processing scripts for language server runtime types.
4+
5+
## Structure
6+
7+
- `schema/` - OpenAPI schema definitions
8+
- `custom-templates/` - Custom Mustache templates for code generation sorted by langauage
9+
- `generated/` - Generated output directory where TypeScript and Java types will be located post generation
10+
- `post-generate.js` - Post-processing script
11+
- `openapitools.json` - OpenAPI Generator configuration
12+
13+
## Configuration
14+
15+
### openapitools.json
16+
17+
OpenAPI Generator configuration defining code generation settings for TypeScript and Java:
18+
19+
**Common Configuration:**
20+
- `generatorName` - Specifies the generator type (typescript-fetch, java)
21+
- `disabled` - Allows selective turning off of generators during development. By default, all generators run. New languages/input specs can be added with their own customization
22+
- `output` - Target directory for generated code
23+
- `inputSpec` - Source OpenAPI schema file
24+
- `templateDir` - Points to custom Mustache templates that overwrite canonical templates from [OpenAPI Generator templates](https://openapi-generator.tech/docs/templating)
25+
26+
**TypeScript Configuration:**
27+
- `additionalProperties` - Language-specific options from the [typescript-fetch generator](https://openapi-generator.tech/docs/generators/typescript-fetch/). Notable settings:
28+
- `stringEnums` - Generates string enums instead of numeric
29+
- `withoutRuntimeChecks` - Reduces unnecessary generated code by removing runtime validation
30+
- `supportsES6` - Enables ES6 features
31+
- `modelPropertyNaming`/`enumPropertyNaming` - Sets camelCase naming conventions
32+
- `global-property` - [Global properties](https://openapi-generator.tech/docs/globals) for debugging and selective model generation (alternative to .openapi-generator-ignore file)
33+
- `openapi-normalizer` - [Normalizers](https://openapi-generator.tech/docs/customization/#openapi-normalizer) shape input before generation:
34+
- `REF_AS_PARENT_IN_ALLOF` - Enables traditional inheritance (extends) via allOf instead of including all parent fields in child, resulting in cleaner code
35+
- `reservedWordsMappings` - Overcomes generator renaming of reserved words. Forces `export` field to remain `export` instead of being renamed to `_export`
36+
- `typeMappings` - Overcomes generator quirks where `Date` gets converted to `string` or `ModelDate`. Forces generator to use proper `Date` type
37+
38+
**Java Configuration:** TODO TODO TODO
39+
- `additionalProperties` - Java-specific settings including Java 21 compatibility, validation options, and serialization settings
40+
- `global-property.models` - Specifies exact models to generate (CursorPosition, FileParams, etc.) for selective generation
41+
- `importMappings` - Maps external types to LSP4J classes for seamless integration with Language Server Protocol
42+
43+
## Post-Processing
44+
45+
The `post-generate.js` script runs after OpenAPI code generation to customize the generated TypeScript types:
46+
47+
- **Adds constants**: Adds protocol method constants from `constants.ts` to the generated index file. This is due to a limitation of the OpenAPI generator which ignores type alias constants in generation.
48+
- **Modifies interfaces**: Changes `PartialResultParams` from exported to internal interface. This is because `PartialResultParams` is only used internally and causes conflicts with VS Code LSP protocol type of the same name if exported. This interface is not exported in the current `chat.ts` either.
49+
- **Extensible**: Other post-processing steps should be added here.
50+
51+
This ensures the generated types integrate properly with the language server runtime while maintaining clean separation between generated and hand-written code.

types/codegen/custom-templates/java/record.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
{{>generatedAnnotation}}
55
public record {{classname}}(
66
{{#vars}}
7-
@JsonProperty("{{baseName}}")
8-
{{#required}}@Nonnull{{/required}}{{^required}}@Nullable{{/required}} {{{datatypeWithEnum}}} {{name}}{{^-last}},{{/-last}}
7+
{{#description}} // {{description}}
8+
{{/description}} @JsonProperty("{{baseName}}")
9+
{{#required}}@Nonnull{{/required}}{{^required}}@Nullable{{/required}} {{{datatypeWithEnum}}} {{name}}{{^-last}},{{/-last}}{{^-last}}
10+
11+
{{/-last}}
912
{{/vars}}
1013
) {{#parent}}implements {{{.}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
1114

types/codegen/openapitools.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@
5050
"targetCompatibility": "21"
5151
},
5252
"global-property": {
53-
"models": "CursorPosition:FileParams:CopyFileParams:OpenFileDiffParams:ShowOpenDialogParams:ShowSaveFileDialogParams:ShowSaveFileDialogResult"
53+
"models": "IconType:ContextCommandGroup:QuickActionCommand:ContextCommand:CursorPosition:FileParams:CopyFileParams:OpenFileDiffParams:ShowOpenDialogParams:ShowSaveFileDialogParams:ShowSaveFileDialogResult"
5454
},
5555
"importMappings": {
5656
"Position": "org.eclipse.lsp4j.Position",
5757
"Range": "org.eclipse.lsp4j.Range",
5858
"TextDocumentIdentifier": "org.eclipse.lsp4j.TextDocumentIdentifier"
59+
},
60+
"typeMappings": {
61+
"IconType": "String",
62+
"Uint8Array": "byte[]"
5963
}
6064
}
6165
}

types/codegen/post-generate.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,44 @@ const path = require('path')
55

66
// Path to the generated index.ts file
77
const indexPath = path.join(__dirname, 'generated/typescript/src/models/index.ts')
8+
const constantsPath = path.join(__dirname, 'constants.ts')
9+
10+
// Check if files exist
11+
if (!fs.existsSync(constantsPath)) {
12+
console.error('Error: constants.ts file not found')
13+
process.exit(1)
14+
}
15+
16+
if (!fs.existsSync(indexPath)) {
17+
console.error('Error: Generated index.ts file not found')
18+
process.exit(1)
19+
}
820

921
// Read the constants file
10-
const constants = fs.readFileSync(path.join(__dirname, 'constants.ts'), 'utf8')
22+
const constants = fs.readFileSync(constantsPath, 'utf8')
1123

1224
// Read the generated index.ts file
1325
let indexContent = fs.readFileSync(indexPath, 'utf8')
1426

1527
// Find the position after the imports
1628
const importEndPos = indexContent.lastIndexOf('import')
17-
const importEndLinePos = indexContent.indexOf('\n', importEndPos) + 1
29+
let insertPos = 0
1830

19-
// Insert the constants after the imports
20-
const newContent =
21-
indexContent.substring(0, importEndLinePos) +
22-
'\n// Constants\n' +
23-
constants +
24-
'\n' +
25-
indexContent.substring(importEndLinePos)
31+
if (importEndPos !== -1) {
32+
insertPos = indexContent.indexOf('\n', importEndPos) + 1
33+
}
34+
35+
// Insert the constants after the imports (or at the top if no imports)
36+
const newContent = indexContent.substring(0, insertPos) + '\n' + constants + '\n' + indexContent.substring(insertPos)
2637

2738
// Modify PartialResultParams interface
2839
let modifiedContent = newContent.replace(
2940
/export\s+interface\s+PartialResultParams\s*\{/g,
3041
'interface PartialResultParams {'
3142
)
3243

33-
// Replace any fields named "_export" with "export"
34-
//modifiedContent = modifiedContent.replace(/\b_export\??\s*:/g, 'export?:') // For TypeScript interface properties
35-
3644
// Write the updated content back to the file
3745
fs.writeFileSync(indexPath, modifiedContent)
3846

3947
console.log('Constants added to generated index.ts file')
4048
console.log('PartialResultParams interface modified')
41-
//console.log('Renamed "_export" fields to "export"')

types/codegen/schema/chatTypes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@
731731
},
732732
"label": {
733733
"type": "string",
734-
"enum": ["file", "folder", "code", "image"]
734+
"description": "file, folder, code, image"
735735
},
736736
"children": {
737737
"type": "array",

0 commit comments

Comments
 (0)