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

[aspnetcore] Added support for patterns on model properties #2948

Merged
merged 1 commit into from May 29, 2019

Conversation

ingvirafn
Copy link
Contributor

ASPNET Core uses the attribute RegularExpression to validate the model state. This is supported in OpenAPI spec with the 'pattern' property.
This PR adds the reg-ex attribute to the model codegen.

@jimschubert
Copy link
Member

Thanks for the contribution!

I've tested with a petstore simple spec and a pattern added to the Pet model's name:

"pattern": "/[^\\S\\r\\n]/"

Generating code on this branch creates the expected attribute:

/// <summary>
/// Gets or Sets Name
/// </summary>
[Required]
[RegularExpression("/[^\\S\\r\\n]/")]
[DataMember(Name="name", EmitDefaultValue=false)]
public string Name { get; set; }

Putting an invalid name via swagger ui:

{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "this is a name",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}

It responds with the error object:

{
  "errors": {
    "name": [
      "The field Name must match the regular expression '/[^\\S\\r\\n]/'."
    ]
  },
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "0HLN0HUH2OUIH:00000005"
}

I do wonder if here are any concerns with this response object, since it exposes the regex value to the client. Do you know if it's possible to make the error message generic?

@wing328
Copy link
Member

wing328 commented May 26, 2019

I do wonder if here are any concerns with this response object, since it exposes the regex value to the client. Do you know if it's possible to make the error message generic?

To me, it's ok as the error message should be informative so not a bad idea for the client to know why the validation fails.

@ingvirafn
Copy link
Contributor Author

ingvirafn commented May 26, 2019 via email

@wing328
Copy link
Member

wing328 commented May 29, 2019

Let's go with this implementation and see if we've more feedback from the users on this.

@wing328 wing328 merged commit 8a6d996 into OpenAPITools:master May 29, 2019
@wing328 wing328 changed the title Added support for patterns on model properties [ASP.Net Core] Added support for patterns on model properties May 31, 2019
@wing328 wing328 changed the title [ASP.Net Core] Added support for patterns on model properties [aspnetcore] Added support for patterns on model properties May 31, 2019
@wing328
Copy link
Member

wing328 commented Jun 3, 2019

@ingvirafn thanks again for your contribution, which has been included in the v4.0.1 release (https://twitter.com/oas_generator/status/1135534738658062336)

jimschubert added a commit to jimschubert/openapi-generator that referenced this pull request Jun 5, 2019
* 4.1.x: (56 commits)
  sync master
  Update compatibility table
  Ruby client: escape path parameters (OpenAPITools#3039)
  [gradle plugin] Release 4.0.1 fixes (OpenAPITools#3051)
  Update version to 4.0.2-SNAPSHOT (OpenAPITools#3047)
  Map number to double time since float is also parsed as double in Qt5 C++ (OpenAPITools#3046)
  Prepare 4.0.1 release (OpenAPITools#3041)
  [gradle] Reworking publishing pipeline (OpenAPITools#2886)
  [typescript-fetch] Fix uploading files (OpenAPITools#2900)
  Resolves OpenAPITools#2962 - Add properties config to Maven parameters (OpenAPITools#2963)
  fix(golang): Check error of xml Encode (OpenAPITools#3027)
  [C++][Restbed] Add handler callback methods (OpenAPITools#2911)
  Remove null checks for C# value types (OpenAPITools#2933)
  [python-server] Support python 3.7 for all server-generators (OpenAPITools#2884)
  Use golang's provided method names (gin) (OpenAPITools#2983)
  [python] Adding constructor parameters to Configuration and improving documentation (OpenAPITools#3002)
  Add new option to maven plugin's readme (OpenAPITools#3025)
  Engine param in maven plugin. (OpenAPITools#2881)
  Added support for patterns on model properties (OpenAPITools#2948)
  [csharp] Make API response headers dictionary case insensitive (OpenAPITools#2998)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants