diff --git a/src/generators/csharp/CSharpPreset.ts b/src/generators/csharp/CSharpPreset.ts index ac66c1c36c..fcd140d3a6 100644 --- a/src/generators/csharp/CSharpPreset.ts +++ b/src/generators/csharp/CSharpPreset.ts @@ -48,7 +48,7 @@ export interface EnumPresetType extends EnumPreset { extensionMethods?: ( args: PresetArgs ) => Promise | string; -}; +} export type CSharpPreset = Preset<{ class: CsharpClassPreset; record: CsharpRecordPreset; diff --git a/src/generators/csharp/constrainer/EnumConstrainer.ts b/src/generators/csharp/constrainer/EnumConstrainer.ts index b5aa0ccefe..561e83a59a 100644 --- a/src/generators/csharp/constrainer/EnumConstrainer.ts +++ b/src/generators/csharp/constrainer/EnumConstrainer.ts @@ -75,11 +75,12 @@ export function defaultEnumKeyConstraints( export function defaultEnumValueConstraints(): CSharpEnumValueConstraint { return ({ enumValue }) => { let normalizedEnumValue; - if(enumValue === null) return enumValue; - if(Array.isArray(enumValue)) return `"${JSON.stringify(enumValue).replace( - /"/g, - '\\"' - )}"`; + if (enumValue === null) { + return enumValue; + } + if (Array.isArray(enumValue)) { + return `"${JSON.stringify(enumValue).replace(/"/g, '\\"')}"`; + } switch (typeof enumValue) { case 'boolean': case 'bigint': diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts index 2f5d678c6d..78d3df91fe 100644 --- a/src/generators/csharp/presets/JsonSerializerPreset.ts +++ b/src/generators/csharp/presets/JsonSerializerPreset.ts @@ -125,7 +125,7 @@ function renderDeserializeProperty(model: ConstrainedObjectPropertyModel) { ) { return `JsonSerializer.Deserialize<${model.property.name}>(ref reader)`; } - + return `JsonSerializer.Deserialize<${model.property.type}>(ref reader)`; } @@ -250,67 +250,69 @@ ${renderer.indent(serialize)} } }, enum: { - self({content, renderer}) { - renderer.dependencyManager.addDependency('using System.Text.Json;') + self({ content, renderer }) { + renderer.dependencyManager.addDependency('using System.Text.Json;'); return content; }, - extensionMethods({content, model, renderer}) { + extensionMethods({ content, model, renderer }) { const enums = model.values || []; const items: string[] = []; const items2: string[] = []; for (const enumValue of enums) { - let jsonValue = enumValue.value; + const jsonValue = enumValue.value; const originalEnumValue = enumValue.originalInput; let stringValue = jsonValue; - if(typeof jsonValue !== 'string') stringValue = `"${jsonValue}"`; + if (typeof jsonValue !== 'string') { + stringValue = `"${jsonValue}"`; + } items.push( `case ${model.name}.${enumValue.key}: return ${stringValue};` ); - if(typeof originalEnumValue === 'string'){ + if (typeof originalEnumValue === 'string') { items2.push( `if (value?.ValueKind == JsonValueKind.String && value?.GetString() == ${enumValue.value}) { return ${model.name}.${enumValue.key}; }` ); - } else if(originalEnumValue === null){ + } else if (originalEnumValue === null) { items2.push( `if (value == null || value?.ValueKind == JsonValueKind.Null && value?.GetRawText() == "null") { return ${model.name}.${enumValue.key}; }` ); - } else if(typeof originalEnumValue === 'boolean'){ + } else if (typeof originalEnumValue === 'boolean') { items2.push( `if (value?.ValueKind == JsonValueKind.True || value?.ValueKind == JsonValueKind.False && value?.GetBoolean() == ${enumValue.value}) { return ${model.name}.${enumValue.key}; }` ); - } else if(Array.isArray(originalEnumValue)){ + } else if (Array.isArray(originalEnumValue)) { items2.push( `if (value?.ValueKind == JsonValueKind.Array && value?.GetRawText() == ${enumValue.value}) { return ${model.name}.${enumValue.key}; }` ); - } else if(typeof originalEnumValue === 'object'){ + } else if (typeof originalEnumValue === 'object') { items2.push( `if (value?.ValueKind == JsonValueKind.Object && value?.GetRawText() == ${enumValue.value}) { return ${model.name}.${enumValue.key}; }` ); - } else if(typeof originalEnumValue === 'number'){ + } else if (typeof originalEnumValue === 'number') { items2.push( `if (value?.ValueKind == JsonValueKind.Number && value?.GetInt32() == ${enumValue.value}) { return ${model.name}.${enumValue.key}; }` ); - } else if(typeof originalEnumValue === 'bigint'){ + } else if (typeof originalEnumValue === 'bigint') { items2.push( `if (value?.ValueKind == JsonValueKind.Number && value?.GetInt64() == ${enumValue.value}) { @@ -338,6 +340,6 @@ public static ${model.type}? FromJsonTo${model.name}(JsonElement? value) ${renderer.indent(newstuff2, 2)} return null; }`; - }, + } } }; diff --git a/src/generators/csharp/renderers/EnumRenderer.ts b/src/generators/csharp/renderers/EnumRenderer.ts index 093153463d..eb41ef9c85 100644 --- a/src/generators/csharp/renderers/EnumRenderer.ts +++ b/src/generators/csharp/renderers/EnumRenderer.ts @@ -86,7 +86,7 @@ export const CSHARP_DEFAULT_ENUM_PRESET: EnumPresetType = { return `public static class ${model.name}Extensions { ${renderer.indent(extensionMethods)} -}` +}`; }, extensionMethods({ model, renderer }) { const getValueCaseItemValues = renderer.getValueCaseItemValues(); @@ -101,9 +101,7 @@ return null;`; ${renderer.indent(toEnumCaseItemValues)} } return null;`; - const classContent = `public static ${model.type}? GetValue(this ${ - model.name - } enumValue) + return `public static ${model.type}? GetValue(this ${model.name} enumValue) { ${renderer.indent(enumValueSwitch)} } @@ -112,6 +110,5 @@ public static ${model.name}? To${model.name}(dynamic? value) { ${renderer.indent(valueSwitch)} }`; - return classContent; } }; diff --git a/src/generators/typescript/index.ts b/src/generators/typescript/index.ts index d50285bd4e..9a7363862b 100644 --- a/src/generators/typescript/index.ts +++ b/src/generators/typescript/index.ts @@ -3,7 +3,7 @@ export * from './TypeScriptFileGenerator'; export { TS_DEFAULT_PRESET } from './TypeScriptPreset'; export type { TypeScriptPreset } from './TypeScriptPreset'; export * from './presets'; -export {RESERVED_TYPESCRIPT_KEYWORDS} from './Constants'; +export { RESERVED_TYPESCRIPT_KEYWORDS } from './Constants'; export { defaultEnumKeyConstraints as typeScriptDefaultEnumKeyConstraints, diff --git a/src/generators/typescript/renderers/TypeRenderer.ts b/src/generators/typescript/renderers/TypeRenderer.ts index 4d75ff840a..a7a2c3c289 100644 --- a/src/generators/typescript/renderers/TypeRenderer.ts +++ b/src/generators/typescript/renderers/TypeRenderer.ts @@ -10,9 +10,7 @@ import { TypeScriptOptions } from '../TypeScriptGenerator'; */ export class TypeRenderer extends TypeScriptRenderer { async defaultSelf(): Promise { - const content = [ - await this.runAdditionalContentPreset() - ]; + const content = [await this.runAdditionalContentPreset()]; return `type ${this.model.name} = ${this.model.type}; diff --git a/test/generators/csharp/CSharpGenerator.spec.ts b/test/generators/csharp/CSharpGenerator.spec.ts index 3678eaecfb..6b6d53e6a4 100644 --- a/test/generators/csharp/CSharpGenerator.spec.ts +++ b/test/generators/csharp/CSharpGenerator.spec.ts @@ -173,7 +173,7 @@ describe('CSharpGenerator', () => { test('should render `enum` type', async () => { const doc = { $id: 'Things', - enum: ['Texas', '1', 1, false, { test: 'test' }, ["test", 1], null] + enum: ['Texas', '1', 1, false, { test: 'test' }, ['test', 1], null] }; const models = await generator.generate(doc); expect(models).toHaveLength(1); diff --git a/test/generators/csharp/presets/JsonSerializerPreset.spec.ts b/test/generators/csharp/presets/JsonSerializerPreset.spec.ts index 9317af4591..f877beb774 100644 --- a/test/generators/csharp/presets/JsonSerializerPreset.spec.ts +++ b/test/generators/csharp/presets/JsonSerializerPreset.spec.ts @@ -13,7 +13,7 @@ const doc = { numberProp: { type: 'number' }, enumProp: { $id: 'EnumTest', - enum: ['Some enum String', true, { test: 'test' }, 2, ["test", 1], null] + enum: ['Some enum String', true, { test: 'test' }, 2, ['test', 1], null] }, objectProp: { type: 'object',