Add validation for response examples to make sure keys are media types defined.
See original GitHub issueBefore filling a bug
- have you checked the faq for known issues.
- have you checked existing issues (probably related to #4244)
Describe the bug
Swagger config
{
"swagger": "2.0",
"info": {
"version": "v2",
"title": "API",
"description": ""
},
"host": "example.com",
"basePath": "/rest",
"schemes": [
"https",
"http"
],
"paths": {
"/api/Actions/{id}": {
"get": {
"tags": [
"Actions"
],
"summary": "Get an action by it's identifier.",
"operationId": "Actions_GetOne",
"consumes": [],
"produces": [
"application/json",
"text/json"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "The action identifier (UID)",
"required": true,
"type": "string",
"format": "uuid"
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/ValidationError"
},
"example": {
"Message": "The provided text was too long. Expected < 150 characters",
"Key": "text"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/ResponseMessage"
},
"example": {
"Message": "Restr"
}
},
"401": {
"description": "Unauthorised - a bearer token is required."
}
},
"security": [
{
"oauth2": []
}
]
}
}
},
"definitions": {
"ResponseMessage": {
"type": "object",
"properties": {
"Message": {
"type": "string"
}
}
},
"ValidationError": {
"type": "object",
"properties": {
"Message": {
"type": "string"
},
"Key": {
"type": "string"
}
}
}
}
}
Autorest config
input-file: test.json
verbose: true
debug: true
csharp:
namespace: Example.Client
output-folder: csharp/Client
enable-xml: true
add-credentials: true
Validation via swagger-cli: yarn run swagger-cli validate test.json
:
test.json is valid
yarn run autorest
Error message:
AutoRest code generation utility [cli version: 3.3.2; node: v12.22.4]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
NOTE: AutoRest core version selected from configuration: ~3.1.0.
Network Enabled: true
Loading AutoRest core 'C:\Users\twsl\.autorest\@autorest_core@3.1.3\node_modules\@autorest\core\dist' (3.1.3)
INFORMATION: > Loading AutoRest extension '@autorest/csharp' (latest->3.0.0-beta.20210803.1)
INFORMATION: > Loading AutoRest extension '@autorest/modelerfour' (4.19.3->4.19.3)
WARNING (PreCheck/CheckDuplicateSchemas): Checking for duplicate schemas, this could take a (long) while. Run with --verbose for more detail.
C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\modelerfour\dist\main.js - FAILURE {} Error: Not able to process media type Message at this moment.
at ModelerFour.filterMediaTypes (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:1369:21)
at ModelerFour.processResponses (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:2058:36)
at ModelerFour.processOperation (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:1672:10)
at ModelerFour.process (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:2327:14)
at processRequest (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\plugin-modelerfour.ts:23:31)
at C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\libs\extension-base\dist\extension-base.js:55:1
PLUGIN FAILURE: Not able to process media type Message at this moment., Error: Not able to process media type Message at this moment.
at ModelerFour.filterMediaTypes (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:1369:21)
at ModelerFour.processResponses (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:2058:36)
at ModelerFour.processOperation (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:1672:10)
at ModelerFour.process (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\modelerfour.ts:2327:14)
at processRequest (C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\@autorest\src\modeler\plugin-modelerfour.ts:23:31)
at C:\Users\twsl\.autorest\@autorest_modelerfour@4.19.3\node_modules\libs\extension-base\dist\extension-base.js:55:1, {}
FATAL: Error: Not able to process media type Message at this moment.
C:\Users\twsl\.autorest\@autorest_core@3.1.3\node_modules\@autorest\core\dist\src_lib_autorest-core_ts.js - FAILURE {}
Error: Plugin modelerfour reported failure.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Expected behavior Create a simple API client.
Additional context
Autorest version: 3.3.2
Node version: tested with 12.22.4, 14.17.4, 16.6.1
Switching to v3 context via --v3
produces the same results.
At some point with an earlier version of the api and autorest i was able to generate a client successfully. Unfortunately I don’t remember the version of autorest used.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Client-side form validation - Learn web development | MDN
The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this ...
Read more >Response Requirements & Validation - Qualtrics
Content validation: Used to force a certain response based on a certain type of content (such as when you want a respondent to...
Read more >Validating Input | Web Accessibility Initiative (WAI) - W3C
HTML5 defines a range of built-in functionality to validate common types of input, such as email addresses and dates. In some situations, such...
Read more >7 Ways to Validate that Your APIs Are Working Correctly
Request and response traffic can be routed in many different ways, ... For example, the “Content-Type” header provides the MIME type with ...
Read more >Validation features — Cornice 5.0.0 documentation
Let's take an example: we want to make sure the incoming request has an X-Verified ... we define the media types we can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well yes and no. Client generation don’t make use of it now but autorest is not limited to client generation and some other tools might. Either way the main problem is actually due to the fact that autorest convert swagger 2.0 to openapi 3.0 first and during this conversion if you have invalid examples then it creates new responses with those mime types.
For example on editor.swagger.io if you convert your spec it produces this(About the same is produced by autorest): And this is definitely incorrect.
However it could maybe just be a warning that remove the invalid examples instead of a strict error.
Yes, the actual specificiation(can’t link the actual section “Example Object”) define the type of
examples
under response asDict<mimeType, any>
and the content under the mimeType being freeform.Seems like neither editor.swagger.io nor autorest will do validation on that. Should probably change that to prevent this confusing error later down the pipeline.