Skip to content

Commit

Permalink
[csharp-netcore] Better regular expression (#15378)
Browse files Browse the repository at this point in the history
* do not escape regular expression in c#

* update samples

* better code format
  • Loading branch information
wing328 authored May 4, 2023
1 parent 6fa089a commit 182240e
Show file tree
Hide file tree
Showing 76 changed files with 471 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,40 +506,40 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
// https://github.com/OpenAPITools/openapi-generator/issues/12324
// TODO: why do these collections contain different instances?
// fixing allVars should suffice instead of patching every collection
for (CodegenProperty property : model.allVars){
for (CodegenProperty property : model.allVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.vars){
for (CodegenProperty property : model.vars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.readWriteVars){
for (CodegenProperty property : model.readWriteVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.optionalVars){
for (CodegenProperty property : model.optionalVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.parentVars){
for (CodegenProperty property : model.parentVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.requiredVars){
for (CodegenProperty property : model.requiredVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.readOnlyVars){
for (CodegenProperty property : model.readOnlyVars) {
patchProperty(model, property);
}
for (CodegenProperty property : model.nonNullableVars){
for (CodegenProperty property : model.nonNullableVars) {
patchProperty(model, property);
}
}
return processed;
}

private void patchProperty(CodegenModel model, CodegenProperty property){
private void patchProperty(CodegenModel model, CodegenProperty property) {
/**
* Hotfix for this issue
* https://github.com/OpenAPITools/openapi-generator/issues/12155
*/
if (property.dataType.equals("List") && property.getComposedSchemas() != null && property.getComposedSchemas().getAllOf() != null){
* Hotfix for this issue
* https://github.com/OpenAPITools/openapi-generator/issues/12155
*/
if (property.dataType.equals("List") && property.getComposedSchemas() != null && property.getComposedSchemas().getAllOf() != null) {
List<CodegenProperty> composedSchemas = property.getComposedSchemas().getAllOf();
if (composedSchemas.size() == 0) {
return;
Expand Down Expand Up @@ -567,26 +567,28 @@ private void patchProperty(CodegenModel model, CodegenProperty property){
}
}

/** Mitigates https://github.com/OpenAPITools/openapi-generator/issues/13709 */
/**
* Mitigates https://github.com/OpenAPITools/openapi-generator/issues/13709
*/
private void removeCircularReferencesInComposedSchemas(CodegenModel cm) {
cm.anyOf.removeIf(anyOf -> anyOf.equals(cm.classname));
cm.oneOf.removeIf(oneOf -> oneOf.equals(cm.classname));
cm.allOf.removeIf(allOf -> allOf.equals(cm.classname));

CodegenComposedSchemas composedSchemas = cm.getComposedSchemas();
if (composedSchemas != null){
if (composedSchemas != null) {
List<CodegenProperty> anyOf = composedSchemas.getAnyOf();
if (anyOf != null) {
anyOf.removeIf(p -> p.dataType.equals(cm.classname));
}

List<CodegenProperty> oneOf = composedSchemas.getOneOf();
if (oneOf != null){
if (oneOf != null) {
oneOf.removeIf(p -> p.dataType.equals(cm.classname));
}

List<CodegenProperty> allOf = composedSchemas.getAllOf();
if (allOf != null){
if (allOf != null) {
allOf.removeIf(p -> p.dataType.equals(cm.classname));
}
}
Expand All @@ -599,7 +601,7 @@ protected List<Map<String, Object>> buildEnumVars(List<Object> values, String da
// this is needed for enumRefs like OuterEnum marked as nullable and also have string values
// keep isString true so that the index will be used as the enum value instead of a string
// this is inline with C# enums with string values
if ("string?".equals(dataType)){
if ("string?".equals(dataType)) {
enumVars.forEach((enumVar) -> {
enumVar.put("isString", true);
});
Expand Down Expand Up @@ -1086,6 +1088,7 @@ public String toExampleValue(Schema p) {

/**
* Return the default value of the property
*
* @param p OpenAPI property object
* @return string presentation of the default value of the property
*/
Expand Down Expand Up @@ -1272,9 +1275,13 @@ public String toModelTestFilename(String name) {
return toModelName(name) + "Tests";
}

public void setLicenseUrl(String licenseUrl) {this.licenseUrl = licenseUrl;}
public void setLicenseUrl(String licenseUrl) {
this.licenseUrl = licenseUrl;
}

public void setLicenseName(String licenseName) {this.licenseName = licenseName;}
public void setLicenseName(String licenseName) {
this.licenseName = licenseName;
}

public void setPackageName(String packageName) {
this.packageName = packageName;
Expand Down Expand Up @@ -1328,12 +1335,12 @@ public String getInterfacePrefix() {
return interfacePrefix;
}

public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag){
public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag) {
this.nullReferenceTypesFlag = nullReferenceTypesFlag;
additionalProperties.put("nullableReferenceTypes", nullReferenceTypesFlag);
additionalProperties.put("nrt", nullReferenceTypesFlag);

if (nullReferenceTypesFlag){
if (nullReferenceTypesFlag) {
additionalProperties.put("nrt?", "?");
additionalProperties.put("nrt!", "!");
} else {
Expand All @@ -1342,7 +1349,7 @@ public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag){
}
}

public boolean getNullableReferencesTypes(){
public boolean getNullableReferencesTypes() {
return this.nullReferenceTypesFlag;
}

Expand Down Expand Up @@ -1442,7 +1449,7 @@ public boolean isDataTypeString(String dataType) {
*/

protected boolean isValueType(CodegenProperty var) {
return (valueTypes.contains(var.dataType) || var.isEnum ) ;
return (valueTypes.contains(var.dataType) || var.isEnum);
}

@Override
Expand Down Expand Up @@ -1581,8 +1588,8 @@ public void postProcessParameter(CodegenParameter parameter) {
// use isNullable, OptionalParameterLambda, or RequiredParameterLambda
if (!parameter.required && (nullReferenceTypesFlag || nullableType.contains(parameter.dataType))) {
parameter.dataType = parameter.dataType.endsWith("?")
? parameter.dataType
: parameter.dataType + "?";
? parameter.dataType
: parameter.dataType + "?";
}
}

Expand Down Expand Up @@ -1617,5 +1624,12 @@ public void postProcessFile(File file, String fileType) {
}

@Override
public GeneratorLanguage generatorLanguage() { return GeneratorLanguage.C_SHARP; }
public GeneratorLanguage generatorLanguage() {
return GeneratorLanguage.C_SHARP;
}

public String toRegularExpression(String pattern) {
return addRegularExpressionDelimiter(pattern);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace {{packageName}}.Client
/// <returns>Filename</returns>
public static string SanitizeFilename(string filename)
{
Match match = Regex.Match(filename, ".*[/\\](.*)$");
Match match = Regex.Match(filename, @".*[/\\](.*)$");
return match.Success ? match.Groups[1].Value : filename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <returns>Filename</returns>
public static string SanitizeFilename(string filename)
{
Match match = Regex.Match(filename, ".*[/\\](.*)$");
Match match = Regex.Match(filename, @".*[/\\](.*)$");
return match.Success ? match.Groups[1].Value : filename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
{{#pattern}}
{{^isByteArray}}
// {{{name}}} ({{{dataType}}}) pattern
Regex regex{{{name}}} = new Regex("{{{vendorExtensions.x-regex}}}"{{#vendorExtensions.x-modifiers}}{{#-first}}, {{/-first}}RegexOptions.{{{.}}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}});
Regex regex{{{name}}} = new Regex(@"{{{vendorExtensions.x-regex}}}"{{#vendorExtensions.x-modifiers}}{{#-first}}, {{/-first}}RegexOptions.{{{.}}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}});
if (false == regex{{{name}}}.Match(this.{{{name}}}{{#isUuid}}.ToString(){{/isUuid}}).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, must match a pattern of " + regex{{{name}}}, new [] { "{{{name}}}" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,10 @@ components:
description: A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
type: string
pattern: '/^image_\d{1,3}$/i'
pattern_with_backslash:
description: None
type: string
pattern: '^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$'
EnumClass:
type: string
default: '-efg'
Expand Down Expand Up @@ -2268,4 +2272,4 @@ components:
default: C:\\Users\\username
unescapedLiteralString:
type: string
default: C:\Users\username
default: C:\Users\username
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static ClientUtils()
/// <returns>Filename</returns>
public static string SanitizeFilename(string filename)
{
Match match = Regex.Match(filename, ".*[/\\](.*)$");
Match match = Regex.Match(filename, @".*[/\\](.*)$");
return match.Success ? match.Groups[1].Value : filename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,11 @@ components:
to three digits following i.e. Image_01.
pattern: "/^image_\\d{1,3}$/i"
type: string
pattern_with_backslash:
description: None
pattern: "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\\
/([0-9]|[1-2][0-9]|3[0-2]))$"
type: string
required:
- byte
- date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Name | Type | Description | Notes
**Password** | **string** | |
**PatternWithDigits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
**PatternWithDigitsAndDelimiter** | **string** | A string starting with &#39;image_&#39; (case insensitive) and one to three digits following i.e. Image_01. | [optional]
**PatternWithBackslash** | **string** | None | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static ClientUtils()
/// <returns>Filename</returns>
public static string SanitizeFilename(string filename)
{
Match match = Regex.Match(filename, ".*[/\\](.*)$");
Match match = Regex.Match(filename, @".*[/\\](.*)$");
return match.Success ? match.Groups[1].Value : filename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ public override int GetHashCode()
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
// Cultivar (string) pattern
Regex regexCultivar = new Regex("^[a-zA-Z\\s]*$", RegexOptions.CultureInvariant);
Regex regexCultivar = new Regex(@"^[a-zA-Z\s]*$", RegexOptions.CultureInvariant);
if (false == regexCultivar.Match(this.Cultivar).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" });
}

// Origin (string) pattern
Regex regexOrigin = new Regex("^[A-Z\\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Regex regexOrigin = new Regex(@"^[A-Z\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (false == regexOrigin.Match(this.Origin).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" });
Expand Down
Loading

0 comments on commit 182240e

Please sign in to comment.