Fix a bug

This commit is contained in:
Andrey Pokhilko
2022-02-21 22:20:45 +03:00
parent 36acd0522f
commit 498816f81d
3 changed files with 77 additions and 118 deletions

View File

@@ -544,7 +544,7 @@
"time": 1, "time": 1,
"request": { "request": {
"method": "GET", "method": "GET",
"url": "https://httpbin.org/param-patterns/prefix-fine", "url": "https://httpbin.org/param-patterns/prefix-gibberish-fine",
"httpVersion": "", "httpVersion": "",
"cookies": [], "cookies": [],
"headers": [ "headers": [

View File

@@ -2,7 +2,7 @@
"openapi": "3.1.0", "openapi": "3.1.0",
"info": { "info": {
"title": "https://httpbin.org", "title": "https://httpbin.org",
"description": "Mizu observed 19 entries (0 failed), at 0.106 hits/s, average response time is 0.172 seconds", "description": "Mizu observed 18 entries (0 failed), at 0.112 hits/s, average response time is 0.182 seconds",
"version": "1.0" "version": "1.0"
}, },
"servers": [ "servers": [
@@ -15,7 +15,7 @@
"get": { "get": {
"summary": "/appears-once", "summary": "/appears-once",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds",
"operationId": "eb3ca1f2-366b-4655-ab0f-e2c180e4c5fd", "operationId": "8afccadd-04ac-4eb9-bd5e-51fda91d7150",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -52,7 +52,7 @@
"get": { "get": {
"summary": "/appears-twice", "summary": "/appears-twice",
"description": "Mizu observed 2 entries (0 failed), at 0.500 hits/s, average response time is 0.630 seconds", "description": "Mizu observed 2 entries (0 failed), at 0.500 hits/s, average response time is 0.630 seconds",
"operationId": "d7e097be-9c43-4498-a696-1c1f2e1b09bf", "operationId": "dd90201a-0d82-4e1f-b20b-de5053bfd07e",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -63,6 +63,16 @@
} }
} }
}, },
"x-counters-per-source": {
"": {
"entries": 2,
"failures": 0,
"firstSeen": 1567750580.7471218,
"lastSeen": 1567750581.7471218,
"sumRT": 1.26,
"sumDuration": 1
}
},
"x-sample-entry": 0, "x-sample-entry": 0,
"x-last-seen-ts": 1567750581.7471218, "x-last-seen-ts": 1567750581.7471218,
"x-counters-total": { "x-counters-total": {
@@ -72,16 +82,6 @@
"lastSeen": 1567750581.7471218, "lastSeen": 1567750581.7471218,
"sumRT": 1.26, "sumRT": 1.26,
"sumDuration": 1 "sumDuration": 1
},
"x-counters-per-source": {
"": {
"entries": 2,
"failures": 0,
"firstSeen": 1567750580.7471218,
"lastSeen": 1567750581.7471218,
"sumRT": 1.26,
"sumDuration": 1
}
} }
} }
}, },
@@ -89,7 +89,7 @@
"post": { "post": {
"summary": "/body-optional", "summary": "/body-optional",
"description": "Mizu observed 3 entries (0 failed), at 0.003 hits/s, average response time is 0.001 seconds", "description": "Mizu observed 3 entries (0 failed), at 0.003 hits/s, average response time is 0.001 seconds",
"operationId": "69e1d67b-8642-48c9-ab2c-221057718e2d", "operationId": "c7521562-c613-445c-a340-a7505311967c",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -108,6 +108,7 @@
"sumDuration": 0.010000228881835938 "sumDuration": 0.010000228881835938
} }
}, },
"x-sample-entry": 0,
"x-last-seen-ts": 1567750581.757122, "x-last-seen-ts": 1567750581.757122,
"x-counters-total": { "x-counters-total": {
"entries": 3, "entries": 3,
@@ -117,7 +118,6 @@
"sumRT": 0.003, "sumRT": 0.003,
"sumDuration": 0.010000228881835938 "sumDuration": 0.010000228881835938
}, },
"x-sample-entry": 0,
"requestBody": { "requestBody": {
"description": "Generic request body", "description": "Generic request body",
"content": { "content": {
@@ -132,7 +132,7 @@
"post": { "post": {
"summary": "/body-required", "summary": "/body-required",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds",
"operationId": "4903b45c-d1d4-43d4-b029-7dde75f14cc6", "operationId": "8b645376-01c8-4666-8d6f-c9b231bf662f",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -150,7 +150,6 @@
"sumRT": 0.001, "sumRT": 0.001,
"sumDuration": 0 "sumDuration": 0
}, },
"x-sample-entry": 0,
"x-counters-per-source": { "x-counters-per-source": {
"": { "": {
"entries": 1, "entries": 1,
@@ -161,6 +160,7 @@
"sumDuration": 0 "sumDuration": 0
} }
}, },
"x-sample-entry": 0,
"requestBody": { "requestBody": {
"description": "Generic request body", "description": "Generic request body",
"content": { "content": {
@@ -176,7 +176,7 @@
"post": { "post": {
"summary": "/form-multipart", "summary": "/form-multipart",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds",
"operationId": "8c19c00b-db51-4eee-bbba-3e15d35ab7b9", "operationId": "d50e532c-9d93-43e0-b626-74e994fe8d2f",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -244,7 +244,7 @@
"post": { "post": {
"summary": "/form-urlencoded", "summary": "/form-urlencoded",
"description": "Mizu observed 2 entries (0 failed), at 0.500 hits/s, average response time is 0.001 seconds", "description": "Mizu observed 2 entries (0 failed), at 0.500 hits/s, average response time is 0.001 seconds",
"operationId": "1e270769-e6a6-4e1a-a541-38423b9a2fa4", "operationId": "fcb6f8ac-2e90-49a0-866a-be0cb5eaefe8",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -253,6 +253,15 @@
} }
} }
}, },
"x-counters-total": {
"entries": 2,
"failures": 0,
"firstSeen": 1567750580.7471218,
"lastSeen": 1567750581.7471218,
"sumRT": 0.002,
"sumDuration": 1
},
"x-sample-entry": 0,
"x-counters-per-source": { "x-counters-per-source": {
"": { "": {
"entries": 2, "entries": 2,
@@ -263,16 +272,7 @@
"sumDuration": 1 "sumDuration": 1
} }
}, },
"x-sample-entry": 0,
"x-last-seen-ts": 1567750581.7471218, "x-last-seen-ts": 1567750581.7471218,
"x-counters-total": {
"entries": 2,
"failures": 0,
"firstSeen": 1567750580.7471218,
"lastSeen": 1567750581.7471218,
"sumRT": 0.002,
"sumDuration": 1
},
"requestBody": { "requestBody": {
"description": "Generic request body", "description": "Generic request body",
"content": { "content": {
@@ -319,14 +319,11 @@
} }
} }
}, },
"/param-patterns/prefix-fine": { "/param-patterns/{parampatternId}": {
"get": { "get": {
"tags": [ "summary": "/param-patterns/{parampatternId}",
"param-patterns" "description": "Mizu observed 5 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds",
], "operationId": "7a93df55-1f10-421b-9d9a-2f3b5430cb75",
"summary": "/param-patterns/prefix-fine",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds",
"operationId": "34b05cea-6bb2-411d-8f9b-f285a2770757",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -335,42 +332,14 @@
} }
} }
}, },
"x-counters-total": {
"entries": 1,
"failures": 0,
"firstSeen": 1567750582,
"lastSeen": 1567750582,
"sumRT": 0.001,
"sumDuration": 0
},
"x-counters-per-source": { "x-counters-per-source": {
"": { "": {
"entries": 1, "entries": 5,
"failures": 0, "failures": 0,
"firstSeen": 1567750582, "firstSeen": 1567750582.000001,
"lastSeen": 1567750582, "lastSeen": 1567750582.000004,
"sumRT": 0.001, "sumRT": 0.005,
"sumDuration": 0 "sumDuration": 0.0000030994415283203125
}
},
"x-last-seen-ts": 1567750582,
"x-sample-entry": 0
}
},
"/param-patterns/{parampatternId}": {
"get": {
"tags": [
"param-patterns"
],
"summary": "/param-patterns/{parampatternId}",
"description": "Mizu observed 5 entries (0 failed), at 0.000 hits/s, average response time is 0.001 seconds",
"operationId": "50a499dc-8fa1-43b2-9db5-ae20405e4e98",
"responses": {
"200": {
"description": "Successful call with status 200",
"content": {
"": {}
}
} }
}, },
"x-sample-entry": 0, "x-sample-entry": 0,
@@ -382,16 +351,6 @@
"lastSeen": 1567750582.000004, "lastSeen": 1567750582.000004,
"sumRT": 0.005, "sumRT": 0.005,
"sumDuration": 0.0000030994415283203125 "sumDuration": 0.0000030994415283203125
},
"x-counters-per-source": {
"": {
"entries": 5,
"failures": 0,
"firstSeen": 1567750582.000001,
"lastSeen": 1567750582.000004,
"sumRT": 0.005,
"sumDuration": 0.0000030994415283203125
}
} }
}, },
"parameters": [ "parameters": [
@@ -428,7 +387,7 @@
"get": { "get": {
"summary": "/{Id}", "summary": "/{Id}",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds",
"operationId": "1f1d5803-b1c9-4732-92ab-ebcc257cd08c", "operationId": "94a28a59-298a-49bd-baac-a9a32ecda7b1",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -439,6 +398,7 @@
} }
} }
}, },
"x-sample-entry": 0,
"x-last-seen-ts": 1567750579.7471218, "x-last-seen-ts": 1567750579.7471218,
"x-counters-total": { "x-counters-total": {
"entries": 1, "entries": 1,
@@ -457,8 +417,7 @@
"sumRT": 0.63, "sumRT": 0.63,
"sumDuration": 0 "sumDuration": 0
} }
}, }
"x-sample-entry": 0
}, },
"parameters": [ "parameters": [
{ {
@@ -484,7 +443,7 @@
"get": { "get": {
"summary": "/{Id}/sub1", "summary": "/{Id}/sub1",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.111 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.111 seconds",
"operationId": "21f3b102-b4c3-472f-b868-038d4a824dac", "operationId": "17fee091-dc09-403d-b926-6ed62e9e1e00",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -493,8 +452,6 @@
} }
} }
}, },
"x-sample-entry": 0,
"x-last-seen-ts": 1567750483.864529,
"x-counters-total": { "x-counters-total": {
"entries": 1, "entries": 1,
"failures": 0, "failures": 0,
@@ -512,7 +469,9 @@
"sumRT": 0.111, "sumRT": 0.111,
"sumDuration": 0 "sumDuration": 0
} }
} },
"x-sample-entry": 0,
"x-last-seen-ts": 1567750483.864529
}, },
"parameters": [ "parameters": [
{ {
@@ -538,7 +497,7 @@
"get": { "get": {
"summary": "/{Id}/sub2", "summary": "/{Id}/sub2",
"description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds", "description": "Mizu observed 1 entries (0 failed), at 0.000 hits/s, average response time is 0.630 seconds",
"operationId": "f4fc84f6-d5f1-48a0-b45d-57fb907467c3", "operationId": "811cd779-8b9d-46f6-9630-b0d23e3a6f7d",
"responses": { "responses": {
"200": { "200": {
"description": "Successful call with status 200", "description": "Successful call with status 200",
@@ -549,14 +508,6 @@
} }
} }
}, },
"x-counters-total": {
"entries": 1,
"failures": 0,
"firstSeen": 1567750578.7471218,
"lastSeen": 1567750578.7471218,
"sumRT": 0.63,
"sumDuration": 0
},
"x-counters-per-source": { "x-counters-per-source": {
"": { "": {
"entries": 1, "entries": 1,
@@ -568,7 +519,15 @@
} }
}, },
"x-sample-entry": 0, "x-sample-entry": 0,
"x-last-seen-ts": 1567750578.7471218 "x-last-seen-ts": 1567750578.7471218,
"x-counters-total": {
"entries": 1,
"failures": 0,
"firstSeen": 1567750578.7471218,
"lastSeen": 1567750578.7471218,
"sumRT": 0.63,
"sumDuration": 0
}
}, },
"parameters": [ "parameters": [
{ {
@@ -592,20 +551,20 @@
} }
}, },
"x-counters-total": { "x-counters-total": {
"entries": 19, "entries": 18,
"failures": 0, "failures": 0,
"firstSeen": 1567750483.864529, "firstSeen": 1567750483.864529,
"lastSeen": 1567750582.7471218, "lastSeen": 1567750582.7471218,
"sumRT": 3.274, "sumRT": 3.2729999999999997,
"sumDuration": 2.0100033283233643 "sumDuration": 2.0100033283233643
}, },
"x-counters-per-source": { "x-counters-per-source": {
"": { "": {
"entries": 19, "entries": 18,
"failures": 0, "failures": 0,
"firstSeen": 1567750483.864529, "firstSeen": 1567750483.864529,
"lastSeen": 1567750582.7471218, "lastSeen": 1567750582.7471218,
"sumRT": 3.274, "sumRT": 3.2729999999999997,
"sumDuration": 2.0100033283233643 "sumDuration": 2.0100033283233643
} }
} }

View File

@@ -124,7 +124,7 @@ func getPatternFromExamples(exmp *openapi.Examples) *openapi.Regexp {
suffix := longestCommonXfixStr(strs, false) suffix := longestCommonXfixStr(strs, false)
pat := "" pat := ""
separators := "-._/:|*,+" separators := "-._/:|*,+" // TODO: we could also cut prefix till the last separator
if len(prefix) > 0 && strings.Contains(separators, string(prefix[len(prefix)-1])) { if len(prefix) > 0 && strings.Contains(separators, string(prefix[len(prefix)-1])) {
pat = "^" + regexp.QuoteMeta(prefix) pat = "^" + regexp.QuoteMeta(prefix)
} }
@@ -228,27 +228,25 @@ func (n *Node) compact() {
if param != nil { if param != nil {
// take its regex // take its regex
pRegex := param.pathParam.Schema.Pattern pRegex := param.pathParam.Schema.Pattern
if pRegex == nil { if pRegex != nil {
return newChildren := make([]*Node, 0)
}
newChildren := make([]*Node, 0) // compact the constants via regex
for _, subnode := range n.children {
// compact the constants via regex if subnode.constant != nil {
for _, subnode := range n.children { if pRegex.Match([]byte(*subnode.constant)) {
if subnode.constant != nil { param.merge(subnode)
if pRegex.Match([]byte(*subnode.constant)) { continue
param.merge(subnode) }
continue
} }
newChildren = append(newChildren, subnode)
} }
newChildren = append(newChildren, subnode)
}
if len(n.children) != len(newChildren) { if len(n.children) != len(newChildren) {
logger.Log.Debugf("Shrinking children from %d to %d", len(n.children), len(newChildren)) logger.Log.Debugf("Shrinking children from %d to %d", len(n.children), len(newChildren))
n.children = newChildren n.children = newChildren
n.compact() n.compact()
}
} }
} }
@@ -346,6 +344,8 @@ func (n *Node) countParentParams() int {
} }
func (n *Node) merge(other *Node) { func (n *Node) merge(other *Node) {
// TODO: merge operations, remember historical operationIDs
// TODO: if n is param and other is constant, could have added an example
outer: outer:
for _, oChild := range other.children { for _, oChild := range other.children {
for _, nChild := range n.children { for _, nChild := range n.children {