Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: special cases in enum values for C# generator #559

Merged
merged 4 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/generators/csharp/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CSharpRenderer } from '../CSharpRenderer';
import { EnumPreset } from '../../../models';
import { pascalCase } from 'change-case';
import { FormatHelpers } from '../../../helpers';

/**
* Renderer for C#'s `enum` type
Expand Down Expand Up @@ -107,7 +108,7 @@ export const CSHARP_DEFAULT_ENUM_PRESET: EnumPreset<EnumRenderer> = {
return renderer.defaultSelf();
},
item({ item }) {
let itemName = String(item);
let itemName = FormatHelpers.replaceSpecialCharacters(String(item), { exclude: [' '], separator: '_' });
if (typeof item === 'number' || typeof item === 'bigint') {
itemName = `Number_${itemName}`;
} else if (typeof item === 'object') {
Expand Down
31 changes: 31 additions & 0 deletions test/generators/csharp/CSharpGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,36 @@ describe('CSharpGenerator', () => {
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);
});

test('should render enums with translated special characters', async () => {
const doc = {
$id: 'States',
type: 'string',
enum: ['test+', 'test', 'test-', 'test?!', '*test']
};

generator = new CSharpGenerator({ presets: [
{
enum: {
self({ content }) {
return content;
},
}
}
]});
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved

const inputModel = await generator.process(doc);
const model = inputModel.models['States'];

let enumModel = await generator.render(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);

enumModel = await generator.renderEnum(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);
});

test('should render models and their dependencies', async () => {
const doc = {
$id: 'Address',
Expand Down Expand Up @@ -177,6 +207,7 @@ describe('CSharpGenerator', () => {
expect(models[0].result).toMatchSnapshot();
expect(models[1].result).toMatchSnapshot();
});

test('should throw error when reserved keyword is used for package name', async () => {
const doc = {
$id: 'Address',
Expand Down
70 changes: 70 additions & 0 deletions test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,76 @@ public static class StatesExtensions {
"
`;

exports[`CSharpGenerator should render enums with translated special characters 1`] = `
"public enum States {
TestPlus, Test, TestMinus, TestQuestionExclamation, AsteriskTest
}
public static class StatesExtensions {
public static dynamic GetValue(this States enumValue)
{
switch (enumValue)
{
case States.TestPlus: return \\"test+\\";
case States.Test: return \\"test\\";
case States.TestMinus: return \\"test-\\";
case States.TestQuestionExclamation: return \\"test?!\\";
case States.AsteriskTest: return \\"*test\\";
}
return null;
}

public static States? ToStates(dynamic value)
{
switch (value)
{
case \\"test+\\": return States.TestPlus;
case \\"test\\": return States.Test;
case \\"test-\\": return States.TestMinus;
case \\"test?!\\": return States.TestQuestionExclamation;
case \\"*test\\": return States.AsteriskTest;
}
return null;
}
}

"
`;

exports[`CSharpGenerator should render enums with translated special characters 2`] = `
"public enum States {
TestPlus, Test, TestMinus, TestQuestionExclamation, AsteriskTest
}
public static class StatesExtensions {
public static dynamic GetValue(this States enumValue)
{
switch (enumValue)
{
case States.TestPlus: return \\"test+\\";
case States.Test: return \\"test\\";
case States.TestMinus: return \\"test-\\";
case States.TestQuestionExclamation: return \\"test?!\\";
case States.AsteriskTest: return \\"*test\\";
}
return null;
}

public static States? ToStates(dynamic value)
{
switch (value)
{
case \\"test+\\": return States.TestPlus;
case \\"test\\": return States.Test;
case \\"test-\\": return States.TestMinus;
case \\"test?!\\": return States.TestQuestionExclamation;
case \\"*test\\": return States.AsteriskTest;
}
return null;
}
}

"
`;

exports[`CSharpGenerator should render models and their dependencies 1`] = `
"namespace Test.Package
{
Expand Down