This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Work around infinite loop bug when using pattern and minLength in OAS 2 #273
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -166,6 +166,15 @@ const convertSubSchema = (schema, references, swagger) => { | |
| actualSchema.type = 'string'; | ||
| } | ||
|
|
||
| if (schema.pattern && schema.minLength && schema.pattern.startsWith('^[') && schema.pattern.endsWith(']*$')) { | ||
| // If a schema has a minimal length (minLength) > 0 AND there is a regex | ||
| // such as: `^[A-z]*$`, the schema can resolve to an empty string which | ||
| // doesn't match minLength. | ||
| // | ||
| // JSON Schema Faker will fail in that case and get into an infinite loop. | ||
| actualSchema.pattern = schema.pattern.replace('*$', '+$'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to make the modification closer to the invocation of that faker tool?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortuantely not, without copying every schema and traversing over it at the JSON Schema Faker entrypoint. I would like to avoid that, it would be a far more expensive operation. |
||
| } | ||
|
|
||
| if (schema.example) { | ||
| actualSchema.examples = [dereference(schema.example, swagger)]; | ||
| } | ||
|
|
||
This file contains hidden or 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
224 changes: 224 additions & 0 deletions
224
packages/fury-adapter-swagger/test/fixtures/json-body-generation-bad-pattern.json
This file contains hidden or 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,224 @@ | ||
| { | ||
| "element": "parseResult", | ||
| "content": [ | ||
| { | ||
| "element": "category", | ||
| "meta": { | ||
| "classes": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "string", | ||
| "content": "api" | ||
| } | ||
| ] | ||
| }, | ||
| "title": { | ||
| "element": "string", | ||
| "content": "Produces JSON with pattern" | ||
| } | ||
| }, | ||
| "attributes": { | ||
| "version": { | ||
| "element": "string", | ||
| "content": "1.0" | ||
| } | ||
| }, | ||
| "content": [ | ||
| { | ||
| "element": "resource", | ||
| "attributes": { | ||
| "href": { | ||
| "element": "string", | ||
| "content": "/test" | ||
| } | ||
| }, | ||
| "content": [ | ||
| { | ||
| "element": "transition", | ||
| "content": [ | ||
| { | ||
| "element": "httpTransaction", | ||
| "content": [ | ||
| { | ||
| "element": "httpRequest", | ||
| "attributes": { | ||
| "method": { | ||
| "element": "string", | ||
| "content": "GET" | ||
| }, | ||
| "headers": { | ||
| "element": "httpHeaders", | ||
| "content": [ | ||
| { | ||
| "element": "member", | ||
| "meta": { | ||
| "links": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "link", | ||
| "attributes": { | ||
| "relation": { | ||
| "element": "string", | ||
| "content": "inferred" | ||
| }, | ||
| "href": { | ||
| "element": "string", | ||
| "content": "http://docs.apiary.io/validations/swagger#produces-accept" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "content": { | ||
| "key": { | ||
| "element": "string", | ||
| "content": "Accept" | ||
| }, | ||
| "value": { | ||
| "element": "string", | ||
| "content": "application/json" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "element": "httpResponse", | ||
| "attributes": { | ||
| "headers": { | ||
| "element": "httpHeaders", | ||
| "content": [ | ||
| { | ||
| "element": "member", | ||
| "meta": { | ||
| "links": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "link", | ||
| "attributes": { | ||
| "relation": { | ||
| "element": "string", | ||
| "content": "inferred" | ||
| }, | ||
| "href": { | ||
| "element": "string", | ||
| "content": "http://docs.apiary.io/validations/swagger#produces-content-type" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "content": { | ||
| "key": { | ||
| "element": "string", | ||
| "content": "Content-Type" | ||
| }, | ||
| "value": { | ||
| "element": "string", | ||
| "content": "application/json" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| "statusCode": { | ||
| "element": "string", | ||
| "content": "200" | ||
| } | ||
| }, | ||
| "content": [ | ||
| { | ||
| "element": "copy", | ||
| "content": "My Response" | ||
| }, | ||
| { | ||
| "element": "asset", | ||
| "meta": { | ||
| "classes": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "string", | ||
| "content": "messageBody" | ||
| } | ||
| ] | ||
| }, | ||
| "links": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "link", | ||
| "attributes": { | ||
| "relation": { | ||
| "element": "string", | ||
| "content": "inferred" | ||
| }, | ||
| "href": { | ||
| "element": "string", | ||
| "content": "http://docs.apiary.io/validations/swagger#message-body-generation" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "attributes": { | ||
| "contentType": { | ||
| "element": "string", | ||
| "content": "application/json" | ||
| } | ||
| }, | ||
| "content": "A" | ||
| }, | ||
| { | ||
| "element": "asset", | ||
| "meta": { | ||
| "classes": { | ||
| "element": "array", | ||
| "content": [ | ||
| { | ||
| "element": "string", | ||
| "content": "messageBodySchema" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "attributes": { | ||
| "contentType": { | ||
| "element": "string", | ||
| "content": "application/schema+json" | ||
| } | ||
| }, | ||
| "content": "{\"type\":\"string\",\"minLength\":1,\"maxLength\":255,\"pattern\":\"^[A-z]+$\"}" | ||
| }, | ||
| { | ||
| "element": "dataStructure", | ||
| "content": { | ||
| "element": "string", | ||
| "meta": { | ||
| "description": { | ||
| "element": "string", | ||
| "content": "- Matches regex pattern: `^[A-z]*$`\n- Length of string must be less than, or equal to 255\n- Length of string must be greater than, or equal to 1" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
packages/fury-adapter-swagger/test/fixtures/json-body-generation-bad-pattern.yaml
This file contains hidden or 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,17 @@ | ||
| swagger: '2.0' | ||
| info: | ||
| title: Produces JSON with pattern | ||
| version: '1.0' | ||
| produces: | ||
| - application/json | ||
| paths: | ||
| '/test': | ||
| get: | ||
| responses: | ||
| 200: | ||
| description: 'My Response' | ||
| schema: | ||
| type: string | ||
| minLength: 1 | ||
| maxLength: 255 | ||
| pattern: ^[A-z]*$ |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this replacing all occurrences of
*$? From what I understand from this PR we should only modify the end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$refers to the end of the line in regex. So replacing*$(usually would happen once at the end), if it is in the schema multiple times I think it should be fine to replace it.