Skip to content

Commit

Permalink
feat(136): add support for custom methods
Browse files Browse the repository at this point in the history
Adding support for AEP-compliance to 136.
  • Loading branch information
toumorokoshi committed Dec 4, 2024
1 parent 4258595 commit 0bfb532
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 326 deletions.
564 changes: 352 additions & 212 deletions example/bookstore/v1/bookstore.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ service Bookstore {
};
}

// archive a book.
rpc archiveBook ( ArchiveBookRequest ) returns ( ArchiveBookResponse );

// An aep-compliant Create method for book-edition.
rpc CreateBookEdition ( CreateBookEditionRequest ) returns ( BookEdition ) {
option (google.api.http) = {
Expand Down Expand Up @@ -295,6 +298,21 @@ message ApplyBookRequest {
Book book = 10015 [(google.api.field_behavior) = REQUIRED];
}

// Request message for the archive method
message ArchiveBookRequest {
// The globally unique identifier for the resource
string path = 10018 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = { type: "bookstore.example.com/book" }
];
}

// Response message for the archive method
message ArchiveBookResponse {
// Field for success.
bool success = 1;
}

// A Create request for a book-edition resource.
message CreateBookEditionRequest {
// A field for the parent of book-edition
Expand Down
10 changes: 10 additions & 0 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,16 @@
}
}
},
"v1ArchiveBookResponse": {
"type": "object",
"properties": {
"success": {
"type": "boolean",
"description": "Field for success."
}
},
"title": "Response message for the archive method"
},
"v1Book": {
"type": "object",
"properties": {
Expand Down
13 changes: 12 additions & 1 deletion example/bookstore/v1/bookstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ resources:
delete: {}
list:
has_unreachable_resources: true
apply: {} # do not uncomment until there is an AEP on apply.
apply: {}
custom:
- name: "archive"
method_type: POST
request:
object_type:
properties: {}
response:
object_type:
properties:
success:
type: BOOLEAN
# other example resources that might be interesting to add:
# authors, which could be a reference for book
# authors could have a reference to publishers too
Expand Down
39 changes: 39 additions & 0 deletions example/bookstore/v1/bookstore_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,50 @@
}
}
}
},
"/publishers/{publisher}/books/{book}:archive": {
"post": {
"summary": "",
"description": "",
"operationId": "",
"parameters": [
{
"name": "publisher",
"in": "path",
"description": "",
"required": true,
"type": "string"
},
{
"name": "book",
"in": "path",
"description": "",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/book"
}
}
}
}
},
"requestBody": {
"description": "",
"content": {
"application/json": {
"schema": {}
}
},
"required": true
}
}
}
},
"components": {
Expand Down
29 changes: 29 additions & 0 deletions example/bookstore/v1/bookstore_openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -540,5 +540,34 @@ paths:
$ref: '#/components/schemas/book-edition'
description: Successful response
summary: ""
/publishers/{publisher}/books/{book}:archive:
post:
description: ""
operationId: ""
parameters:
- description: ""
in: path
name: publisher
required: true
type: string
- description: ""
in: path
name: book
required: true
type: string
requestBody:
content:
application/json:
schema: {}
description: ""
required: true
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/book'
description: Successful response
summary: ""
servers:
- url: http://localhost:8081
29 changes: 26 additions & 3 deletions parser/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func getOrCreateResource(apiResourceByName map[string]*api.Resource, resourceByN
if !ok {
return nil, fmt.Errorf("resource %q not found", name)
}
schema, err := toOpenAPISchemaFromPropMap(schemaR.Properties)
oasSchema, err := toOpenAPISchemaFromPropMap(schemaR.Properties)
if err != nil {
return nil, err
}
addCommonFieldsToResourceSchema(schema)
addCommonFieldsToResourceSchema(oasSchema)
parents := []*api.Resource{}
apiR := &api.Resource{
Singular: schemaR.Kind,
Plural: schemaR.Plural,
Parents: parents,
Schema: schema,
Schema: oasSchema,
}
methods := schemaR.GetMethods()
if methods.Read != nil {
Expand All @@ -80,6 +80,29 @@ func getOrCreateResource(apiResourceByName map[string]*api.Resource, resourceByN
if methods.Apply != nil {
apiR.ApplyMethod = &api.ApplyMethod{}
}
for _, cm := range methods.Custom {
request, err := toOpenAPISchema(cm.Request)
if err != nil {
return nil, err
}
response, err := toOpenAPISchema(cm.Response)
if err != nil {
return nil, err
}
method := "POST"
switch cm.MethodType {
case schema.Methods_CustomMethod_GET:
method = "GET"
case schema.Methods_CustomMethod_POST:
method = "POST"
}
apiR.CustomMethods = append(apiR.CustomMethods, &api.CustomMethod{
Name: cm.Name,
Method: method,
Request: request,
Response: response,
})
}
for _, p := range schemaR.Parents {
apiP, err := getOrCreateResource(apiResourceByName, resourceByName, p)
if err != nil {
Expand Down
Loading

0 comments on commit 0bfb532

Please sign in to comment.