Skip to content

Commit

Permalink
Merge pull request #31 from cerberauth/sort-rules-by-id
Browse files Browse the repository at this point in the history
feat: sort rules by id
  • Loading branch information
emmanuelgautier authored Jun 22, 2023
2 parents 017df41 + 62a71eb commit a480760
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Here is a Ory Oathkeeper rules output
"methods": [
"GET"
],
"url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]\\x2D=\\?&)+/?)$>"
"url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]?\\x2D?=?\\??&?)+/?)$>"
},
"authenticators": [
{
Expand Down Expand Up @@ -165,6 +165,44 @@ Here is a Ory Oathkeeper rules output
"strip_path": "",
"url": ""
}
},
{
"id": "updateUser",
"version": "",
"description": "This can only be done by the logged in user.",
"match": {
"methods": [
"PUT"
],
"url": "<^(https://api\\.example\\.com)(/users/(?:[[:alnum:]]?\\x2D?=?\\??&?)+/?)$>"
},
"authenticators": [
{
"handler": "noop",
"config": null
}
],
"authorizer": {
"handler": "allow",
"config": null
},
"mutators": [
{
"handler": "noop",
"config": null
}
],
"errors": [
{
"handler": "json",
"config": null
}
],
"upstream": {
"preserve_host": false,
"strip_path": "",
"url": ""
}
}
]
```
Expand Down
8 changes: 8 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"context"
"errors"
"sort"

"github.com/cerberauth/openapi-oathkeeper/authenticator"
"github.com/getkin/kin-openapi/openapi3"
Expand All @@ -22,6 +23,12 @@ type Generator struct {
upstream *rule.Upstream
}

type RulesById []rule.Rule

func (r RulesById) Len() int { return len(r) }
func (r RulesById) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r RulesById) Less(i, j int) bool { return r[i].ID < r[j].ID }

func (g *Generator) computeId(operationId string) string {
if g.PrefixId == "" {
return operationId
Expand Down Expand Up @@ -228,5 +235,6 @@ func (g *Generator) Generate() ([]rule.Rule, error) {
}
}

sort.Sort(RulesById(rules))
return rules, nil
}
101 changes: 100 additions & 1 deletion test/stub/sample.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@
"get": {
"summary": "Get user by ID",
"operationId": "getUserById",
"parameters": [],
"parameters": [
{
"name": "id",
"in": "path",
"description": "The user id. ",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response",
Expand Down Expand Up @@ -47,10 +57,99 @@
]
}
]
},
"put": {
"tags": [
"user"
],
"summary": "Update user",
"description": "This can only be done by the logged in user.",
"operationId": "updateUser",
"parameters": [
{
"name": "id",
"in": "path",
"description": "user id that need to be updated",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"description": "Update an existent user in the store",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/User"
}
},
"application/x-www-form-urlencoded": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
},
"responses": {
"default": {
"description": "successful operation"
}
}
}
}
},
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 10
},
"username": {
"type": "string",
"example": "theUser"
},
"firstName": {
"type": "string",
"example": "John"
},
"lastName": {
"type": "string",
"example": "James"
},
"email": {
"type": "string",
"example": "[email protected]"
},
"password": {
"type": "string",
"example": "12345"
},
"phone": {
"type": "string",
"example": "12345"
},
"userStatus": {
"type": "integer",
"description": "User Status",
"format": "int32",
"example": 1
}
},
"xml": {
"name": "user"
}
}
},
"securitySchemes": {
"openidconnect": {
"type": "openIdConnect",
Expand Down

0 comments on commit a480760

Please sign in to comment.