diff --git a/_examples/api.ridl b/_examples/api.ridl index 1bc8c6d..05dde05 100644 --- a/_examples/api.ridl +++ b/_examples/api.ridl @@ -31,6 +31,28 @@ service ExampleService - GetComplex() => (complex: ComplexType) - GetAllOptional() => (optional: Optional) + - StreamUserNotifications() => stream (StreamNotificationsResponse) + +service ExampleService2 + @deprecated + - Ping() + + # GetUserV2 returns user based on given userID. + @auth:JWT + - GetUserV2(GetUserRequest) => (GetUserResponse) + + # FindUser searches for a user using the given search filter. + # + # The filters are q (string) and active (bool). + # + # The minimal length of the "q" filter is 3 characters. + @auth:JWT + - FindUser(FindUserRequest) => (FindUserResponse) + + # ListUsers returns all users. + @auth:JWT + - ListUsers(ListUsersRequest) => (ListUsersResponse) + - StreamUserNotifications(StreamNotificationsRequest) => stream (StreamNotificationsResponse) service AdminService @@ -90,3 +112,25 @@ struct StreamNotificationsResponse struct Notification - message: string - timestamp: timestamp + +struct GetUserRequest + - header: map + - userID: uint64 + +struct GetUserResponse + - code: uint32 + - user: User + +struct FindUserRequest + - s: SearchFilter + +struct FindUserResponse + - name: string + - user: User + +struct ListUsersRequest + - header: map + +struct ListUsersResponse + - users: []User + diff --git a/_examples/openapi.gen.yaml b/_examples/openapi.gen.yaml index f6e9892..7e30cbb 100644 --- a/_examples/openapi.gen.yaml +++ b/_examples/openapi.gen.yaml @@ -1,4 +1,4 @@ -# example v0.0.1 55eeccc8db95e08d65f0da63acfdcbd92cc1e6fa +# example v0.0.1 7fb0c7ec825a4b11489f18f6be64880a8b72ccee # -- # Code generated by webrpc-gen@v0.31.0 with ../ generator; DO NOT EDIT # @@ -430,6 +430,66 @@ components: type: string timestamp: type: string + GetUserRequest: + type: object + required: + - header + - userID + properties: + header: + type: object + description: 'map' + additionalProperties: + type: string + userID: + type: number + GetUserResponse: + type: object + required: + - code + - user + properties: + code: + type: number + user: + $ref: '#/components/schemas/User' + FindUserRequest: + type: object + required: + - s + properties: + s: + $ref: '#/components/schemas/SearchFilter' + FindUserResponse: + type: object + required: + - name + - user + properties: + name: + type: string + user: + $ref: '#/components/schemas/User' + ListUsersRequest: + type: object + required: + - header + properties: + header: + type: object + description: 'map' + additionalProperties: + type: string + ListUsersResponse: + type: object + required: + - users + properties: + users: + type: array + description: '[]User' + items: + $ref: '#/components/schemas/User' ExampleService_Ping_Request: type: object ExampleService_GetUser_Request: @@ -465,9 +525,6 @@ components: type: object ExampleService_StreamUserNotifications_Request: type: object - properties: - streamNotificationsRequest: - $ref: '#/components/schemas/StreamNotificationsRequest' ExampleService_Ping_Response: type: object ExampleService_GetUser_Response: @@ -511,11 +568,10 @@ components: properties: optional: $ref: '#/components/schemas/Optional' - ExampleService_StreamUserNotifications_Response: + ExampleService2_Ping_Request: + type: object + ExampleService2_Ping_Response: type: object - properties: - streamNotificationsResponse: - $ref: '#/components/schemas/StreamNotificationsResponse' AdminService_ListUsers_Request: type: object AdminService_ListUsers_Response: @@ -848,7 +904,7 @@ paths: content: application/x-ndjson: schema: - $ref: '#/components/schemas/ExampleService_StreamUserNotifications_Response' + $ref: '#/components/schemas/StreamNotificationsResponse' # OpenAPI 3.2.0 streaming (itemSchema) is not supported by 3.1.0 tooling: # itemSchema: # $ref: '#/components/schemas/ExampleService_StreamUserNotifications_Response' @@ -876,6 +932,226 @@ paths: - $ref: '#/components/schemas/ErrorWebrpcServerPanic' - $ref: '#/components/schemas/ErrorWebrpcInternalError' - $ref: '#/components/schemas/ErrorDatabaseDown' + /rpc/ExampleService2/Ping: + post: + operationId: ExampleService2-Ping + tags: ["ExampleService2"] + summary: "Deprecated." + deprecated: true + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ExampleService2_Ping_Request' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/ExampleService2_Ping_Response' + '4XX': + description: Client error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcEndpoint' + - $ref: '#/components/schemas/ErrorWebrpcRequestFailed' + - $ref: '#/components/schemas/ErrorWebrpcBadRoute' + - $ref: '#/components/schemas/ErrorWebrpcBadMethod' + - $ref: '#/components/schemas/ErrorWebrpcBadRequest' + - $ref: '#/components/schemas/ErrorWebrpcClientAborted' + - $ref: '#/components/schemas/ErrorWebrpcStreamLost' + - $ref: '#/components/schemas/ErrorRateLimited' + '5XX': + description: Server error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcBadResponse' + - $ref: '#/components/schemas/ErrorWebrpcServerPanic' + - $ref: '#/components/schemas/ErrorWebrpcInternalError' + - $ref: '#/components/schemas/ErrorDatabaseDown' + /rpc/ExampleService2/GetUserV2: + post: + operationId: ExampleService2-GetUserV2 + tags: ["ExampleService2"] + summary: "GetUserV2 returns user based on given userID." + security: + - JWT: [] + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/GetUserRequest' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/GetUserResponse' + '4XX': + description: Client error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcEndpoint' + - $ref: '#/components/schemas/ErrorWebrpcRequestFailed' + - $ref: '#/components/schemas/ErrorWebrpcBadRoute' + - $ref: '#/components/schemas/ErrorWebrpcBadMethod' + - $ref: '#/components/schemas/ErrorWebrpcBadRequest' + - $ref: '#/components/schemas/ErrorWebrpcClientAborted' + - $ref: '#/components/schemas/ErrorWebrpcStreamLost' + - $ref: '#/components/schemas/ErrorRateLimited' + '5XX': + description: Server error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcBadResponse' + - $ref: '#/components/schemas/ErrorWebrpcServerPanic' + - $ref: '#/components/schemas/ErrorWebrpcInternalError' + - $ref: '#/components/schemas/ErrorDatabaseDown' + /rpc/ExampleService2/FindUser: + post: + operationId: ExampleService2-FindUser + tags: ["ExampleService2"] + summary: "FindUser searches for a user using the given search filter." + security: + - JWT: [] + description: | + + The filters are q (string) and active (bool). + + The minimal length of the "q" filter is 3 characters. + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/FindUserRequest' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/FindUserResponse' + '4XX': + description: Client error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcEndpoint' + - $ref: '#/components/schemas/ErrorWebrpcRequestFailed' + - $ref: '#/components/schemas/ErrorWebrpcBadRoute' + - $ref: '#/components/schemas/ErrorWebrpcBadMethod' + - $ref: '#/components/schemas/ErrorWebrpcBadRequest' + - $ref: '#/components/schemas/ErrorWebrpcClientAborted' + - $ref: '#/components/schemas/ErrorWebrpcStreamLost' + - $ref: '#/components/schemas/ErrorRateLimited' + '5XX': + description: Server error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcBadResponse' + - $ref: '#/components/schemas/ErrorWebrpcServerPanic' + - $ref: '#/components/schemas/ErrorWebrpcInternalError' + - $ref: '#/components/schemas/ErrorDatabaseDown' + /rpc/ExampleService2/ListUsers: + post: + operationId: ExampleService2-ListUsers + tags: ["ExampleService2"] + summary: "ListUsers returns all users." + security: + - JWT: [] + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ListUsersRequest' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/ListUsersResponse' + '4XX': + description: Client error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcEndpoint' + - $ref: '#/components/schemas/ErrorWebrpcRequestFailed' + - $ref: '#/components/schemas/ErrorWebrpcBadRoute' + - $ref: '#/components/schemas/ErrorWebrpcBadMethod' + - $ref: '#/components/schemas/ErrorWebrpcBadRequest' + - $ref: '#/components/schemas/ErrorWebrpcClientAborted' + - $ref: '#/components/schemas/ErrorWebrpcStreamLost' + - $ref: '#/components/schemas/ErrorRateLimited' + '5XX': + description: Server error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcBadResponse' + - $ref: '#/components/schemas/ErrorWebrpcServerPanic' + - $ref: '#/components/schemas/ErrorWebrpcInternalError' + - $ref: '#/components/schemas/ErrorDatabaseDown' + /rpc/ExampleService2/StreamUserNotifications: + post: + operationId: ExampleService2-StreamUserNotifications + tags: ["ExampleService2"] + summary: "" + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/StreamNotificationsRequest' + responses: + '200': + description: OK + content: + application/x-ndjson: + schema: + $ref: '#/components/schemas/StreamNotificationsResponse' + # OpenAPI 3.2.0 streaming (itemSchema) is not supported by 3.1.0 tooling: + # itemSchema: + # $ref: '#/components/schemas/ExampleService2_StreamUserNotifications_Response' + '4XX': + description: Client error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcEndpoint' + - $ref: '#/components/schemas/ErrorWebrpcRequestFailed' + - $ref: '#/components/schemas/ErrorWebrpcBadRoute' + - $ref: '#/components/schemas/ErrorWebrpcBadMethod' + - $ref: '#/components/schemas/ErrorWebrpcBadRequest' + - $ref: '#/components/schemas/ErrorWebrpcClientAborted' + - $ref: '#/components/schemas/ErrorWebrpcStreamLost' + - $ref: '#/components/schemas/ErrorRateLimited' + '5XX': + description: Server error + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/ErrorWebrpcBadResponse' + - $ref: '#/components/schemas/ErrorWebrpcServerPanic' + - $ref: '#/components/schemas/ErrorWebrpcInternalError' + - $ref: '#/components/schemas/ErrorDatabaseDown' /rpc/AdminService/ListUsers: post: operationId: AdminService-ListUsers diff --git a/main.go.tmpl b/main.go.tmpl index a3ff199..a9c99a2 100644 --- a/main.go.tmpl +++ b/main.go.tmpl @@ -106,6 +106,8 @@ components: {{- end -}} {{- range $_, $service := $services}} {{- range $i, $method := $service.Methods}} + {{- $isSuccinctInput := and $method.Succinct (eq (len $method.Inputs) 1) -}} + {{- if not $isSuccinctInput }} {{$service.Name}}_{{$method.Name}}_Request: type: object {{- if gt (len $method.Inputs) 0 }} @@ -116,7 +118,10 @@ components: {{- end -}} {{- end -}} {{- end -}} + {{- end -}} {{- range $_, $method := .Methods}} + {{- $isSuccinctOutput := and $method.Succinct (eq (len $method.Outputs) 1) -}} + {{- if not $isSuccinctOutput }} {{$service.Name}}_{{$method.Name}}_Response: type: object {{- if gt (len $method.Outputs) 0 }} @@ -127,11 +132,14 @@ components: {{- end -}} {{- end -}} {{- end -}} + {{- end -}} {{- end}} paths: {{- range $_, $service := .Services -}} {{- range $_, $method := .Methods}} + {{- $isSuccinctInput := and $method.Succinct (eq (len $method.Inputs) 1) -}} + {{- $isSuccinctOutput := and $method.Succinct (eq (len $method.Outputs) 1) -}} {{- $deprecated := index $method.Annotations "deprecated" }} /rpc/{{$service.Name}}/{{$method.Name}}: post: @@ -162,7 +170,11 @@ paths: content: application/json: schema: + {{- if $isSuccinctInput }} + {{ template "fieldType" dict "Type" (index $method.Inputs 0).Type "TypeMap" $typeMap "Indent" " " }} + {{- else }} $ref: '#/components/schemas/{{$service.Name}}_{{$method.Name}}_Request' + {{- end }} responses: '200': description: OK @@ -170,11 +182,19 @@ paths: {{- if eq $method.StreamOutput false }} application/json: schema: + {{- if $isSuccinctOutput }} + {{ template "fieldType" dict "Type" (index $method.Outputs 0).Type "TypeMap" $typeMap "Indent" " " }} + {{- else }} $ref: '#/components/schemas/{{$service.Name}}_{{$method.Name}}_Response' + {{- end }} {{- else }} application/x-ndjson: schema: + {{- if $isSuccinctOutput }} + {{ template "fieldType" dict "Type" (index $method.Outputs 0).Type "TypeMap" $typeMap "Indent" " " }} + {{- else }} $ref: '#/components/schemas/{{$service.Name}}_{{$method.Name}}_Response' + {{- end }} # OpenAPI 3.2.0 streaming (itemSchema) is not supported by 3.1.0 tooling: # itemSchema: # $ref: '#/components/schemas/{{$service.Name}}_{{$method.Name}}_Response'