-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve property name escaping (#1638)
* don't allow "init", "fromJS" or "toJSON" for TypeScript member name * improve performance by not doing string.Replace unless necessary
- Loading branch information
Showing
5 changed files
with
146 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/NJsonSchema.CodeGeneration.TypeScript.Tests/PropertyNameTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Threading.Tasks; | ||
using NJsonSchema.Annotations; | ||
using NJsonSchema.NewtonsoftJson.Generation; | ||
using VerifyXunit; | ||
using Xunit; | ||
|
||
using static NJsonSchema.CodeGeneration.TypeScript.Tests.VerifyHelper; | ||
|
||
namespace NJsonSchema.CodeGeneration.TypeScript.Tests; | ||
|
||
[UsesVerify] | ||
public class PropertyNameTests | ||
{ | ||
private class TypeWithRestrictedProperties | ||
{ | ||
public string Constructor { get; set; } | ||
public string Init { get; set; } | ||
public string FromJS { get; set; } | ||
public string ToJSON { get; set; } | ||
} | ||
|
||
[Fact] | ||
public async Task When_class_has_restricted_properties_they_are_escaped() | ||
{ | ||
var schema = NewtonsoftJsonSchemaGenerator.FromType<TypeWithRestrictedProperties>(); | ||
|
||
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 4.3m }); | ||
var output = generator.GenerateFile(nameof(TypeWithRestrictedProperties)); | ||
|
||
await Verify(output); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...hots/PropertyNameTests.When_class_has_restricted_properties_they_are_escaped.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export class TypeWithRestrictedProperties implements ITypeWithRestrictedProperties { | ||
constructor_!: string | undefined; | ||
init_!: string | undefined; | ||
fromJS_!: string | undefined; | ||
toJSON_!: string | undefined; | ||
|
||
constructor(data?: ITypeWithRestrictedProperties) { | ||
if (data) { | ||
for (var property in data) { | ||
if (data.hasOwnProperty(property)) | ||
(<any>this)[property] = (<any>data)[property]; | ||
} | ||
} | ||
} | ||
|
||
init(_data?: any) { | ||
if (_data) { | ||
this.constructor_ = _data["Constructor"]; | ||
this.init_ = _data["Init"]; | ||
this.fromJS_ = _data["FromJS"]; | ||
this.toJSON_ = _data["ToJSON"]; | ||
} | ||
} | ||
|
||
static fromJS(data: any): TypeWithRestrictedProperties { | ||
data = typeof data === 'object' ? data : {}; | ||
let result = new TypeWithRestrictedProperties(); | ||
result.init(data); | ||
return result; | ||
} | ||
|
||
toJSON(data?: any) { | ||
data = typeof data === 'object' ? data : {}; | ||
data["Constructor"] = this.constructor_; | ||
data["Init"] = this.init_; | ||
data["FromJS"] = this.fromJS_; | ||
data["ToJSON"] = this.toJSON_; | ||
return data; | ||
} | ||
} | ||
|
||
export interface ITypeWithRestrictedProperties { | ||
constructor_: string | undefined; | ||
init_: string | undefined; | ||
fromJS_: string | undefined; | ||
toJSON_: string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters