Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/generators/csharp/presets/NewtonsoftSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ function renderDeserialize({

const corePropsRead = coreProps
.map((prop) => {
// `const` properties are rendered as read-only (a `const` field or a
// getter-only property), so assigning to them here would not compile
// (CS0200/CS0131). The value is fixed by the schema, so there is nothing
// to deserialize into it.
if (prop.property.options.const) {
return '';
}
const propertyAccessor = pascalCase(prop.propertyName);
let toValue = `jo["${prop.unconstrainedPropertyName}"].ToObject<${prop.property.type}>(serializer)`;
if (
Expand Down
2 changes: 1 addition & 1 deletion src/generators/csharp/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const CSHARP_DEFAULT_CLASS_PRESET: CsharpClassPreset<CSharpOptions> = {
if (property.property.options.const) {
return `public const ${property.property.type} ${pascalCase(
property.propertyName
)} { ${getter} } = ${property.property.options.const.value};`;
)} = ${property.property.options.const.value};`;
}

const semiColon = nullablePropertyEnding !== '' ? ';' : '';
Expand Down
24 changes: 24 additions & 0 deletions test/generators/csharp/CSharpGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ describe('CSharpGenerator', () => {
]);
});

test('should generate a valid const property with autoImplementedProperties', async () => {
const autoImplementGenerator = new CSharpGenerator({
modelType: 'class',
autoImplementedProperties: true
});
const doc = {
$id: 'Event',
type: 'object',
properties: {
eventType: { type: 'string', const: 'OnEntryStarted' }
},
required: ['eventType']
};

const models = await autoImplementGenerator.generate(doc);
expect(models).toHaveLength(1);
// A C# `const` field cannot have `{ get; }` accessors, so it must be
// rendered as a plain const field assignment.
expect(models[0].result).toContain(
'public const string EventType = "OnEntryStarted";'
);
expect(models[0].result).not.toContain('public const string EventType {');
});

test('should render `enum` type', async () => {
const doc = {
$id: 'Things',
Expand Down
12 changes: 12 additions & 0 deletions test/generators/csharp/presets/NewtonsoftSerializerPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ describe('Newtonsoft JSON serializer preset', () => {
expect(outputModels[1].result).toMatchSnapshot();
expect(outputModels[2].result).toMatchSnapshot();
});

test('should not assign to `const` properties in ReadJson', async () => {
const generator = new CSharpGenerator({
presets: [CSHARP_NEWTONSOFT_SERIALIZER_PRESET]
});

const outputModels = await generator.generate(doc);
// `const` properties are rendered as read-only, so assigning to them in
// ReadJson would not compile (CS0200/CS0131). The deserializer must skip
// them.
expect(outputModels[0].result).not.toContain('value.ConstStringProp =');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ public class TestConverter : JsonConverter<Test>
}
value.StringProp = jo[\\"string prop\\"].ToObject<string>(serializer);

if(jo[\\"const string prop\\"] != null) {
value.ConstStringProp = jo[\\"const string prop\\"].ToObject<string?>(serializer);
}
if(jo[\\"notRequiredStringProp\\"] != null) {
value.NotRequiredStringProp = jo[\\"notRequiredStringProp\\"].ToObject<string?>(serializer);
}
Expand Down
Loading