aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-02-24 17:36:24 -0500
committerXe Iaso <me@xeiaso.net>2024-02-24 17:36:24 -0500
commit5b8b30a3f221c21d87cf4174aca5e913136f8399 (patch)
tree1139cc1678e3d190f3d68ad6bd7863e30b2923ef
parent9338a5b621a7404bbb6390200e3ec4590935cc63 (diff)
downloadxesite-5b8b30a3f221c21d87cf4174aca5e913136f8399.tar.xz
xesite-5b8b30a3f221c21d87cf4174aca5e913136f8399.zip
cmd/twirp-openapi-gen: fix empty schema
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--.vscode/extensions.json1
-rw-r--r--cmd/twirp-openapi-gen/internal/generator/aliases.go3
-rw-r--r--cmd/twirp-openapi-gen/internal/generator/handlers.go69
-rw-r--r--pb/generate.go2
-rw-r--r--pb/openapi.json16
5 files changed, 58 insertions, 33 deletions
diff --git a/.vscode/extensions.json b/.vscode/extensions.json
index ad47f27..2c23b01 100644
--- a/.vscode/extensions.json
+++ b/.vscode/extensions.json
@@ -1,7 +1,6 @@
{
"recommendations": [
"unifiedjs.vscode-mdx",
- "golang.go",
"bbenoist.nix",
"jnoortheen.nix-ide",
"dhall.dhall-lang",
diff --git a/cmd/twirp-openapi-gen/internal/generator/aliases.go b/cmd/twirp-openapi-gen/internal/generator/aliases.go
index a8e29f3..660d345 100644
--- a/cmd/twirp-openapi-gen/internal/generator/aliases.go
+++ b/cmd/twirp-openapi-gen/internal/generator/aliases.go
@@ -74,9 +74,6 @@ var typeAliases = map[string]struct {
Type: "boolean",
Format: "boolean",
},
- "google.protobuf.Empty": {
- Type: "object",
- },
"google.type.DateTime": {
Type: "string",
diff --git a/cmd/twirp-openapi-gen/internal/generator/handlers.go b/cmd/twirp-openapi-gen/internal/generator/handlers.go
index c11eb46..219000f 100644
--- a/cmd/twirp-openapi-gen/internal/generator/handlers.go
+++ b/cmd/twirp-openapi-gen/internal/generator/handlers.go
@@ -18,6 +18,7 @@ const (
googleListValueType = "google.protobuf.ListValue"
googleStructType = "google.protobuf.Struct"
googleValueType = "google.protobuf.Value"
+ googleEmptyType = "google.protobuf.Empty"
googleMoneyType = "google.type.Money"
)
@@ -92,40 +93,41 @@ func (gen *generator) RPC(rpc *proto.RPC) {
var reqMediaType *openapi3.MediaType
switch rpc.RequestType {
case "google.protobuf.Empty":
- reqMediaType = openapi3.NewMediaType()
+ gen.addGoogleEmptySchema()
default:
- if strings.Contains(rpc.RequestType, ".") {
- reqMediaType = &openapi3.MediaType{
- Schema: &openapi3.SchemaRef{
- Ref: fmt.Sprintf("#/components/schemas/%s", rpc.RequestType),
- },
- }
- } else {
- reqMediaType = &openapi3.MediaType{
- Schema: &openapi3.SchemaRef{
- Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.RequestType),
- },
- }
+ }
+ if strings.Contains(rpc.RequestType, ".") {
+ reqMediaType = &openapi3.MediaType{
+ Schema: &openapi3.SchemaRef{
+ Ref: fmt.Sprintf("#/components/schemas/%s", rpc.RequestType),
+ },
+ }
+ } else {
+ reqMediaType = &openapi3.MediaType{
+ Schema: &openapi3.SchemaRef{
+ Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.RequestType),
+ },
}
}
var resMediaType *openapi3.MediaType
switch rpc.ReturnsType {
case "google.protobuf.Empty":
- resMediaType = openapi3.NewMediaType()
+ gen.addGoogleEmptySchema()
default:
- if strings.Contains(rpc.ReturnsType, ".") {
- resMediaType = &openapi3.MediaType{
- Schema: &openapi3.SchemaRef{
- Ref: fmt.Sprintf("#/components/schemas/%s", rpc.ReturnsType),
- },
- }
- } else {
- resMediaType = &openapi3.MediaType{
- Schema: &openapi3.SchemaRef{
- Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.ReturnsType),
- },
- }
+ }
+
+ if strings.Contains(rpc.ReturnsType, ".") {
+ resMediaType = &openapi3.MediaType{
+ Schema: &openapi3.SchemaRef{
+ Ref: fmt.Sprintf("#/components/schemas/%s", rpc.ReturnsType),
+ },
+ }
+ } else {
+ resMediaType = &openapi3.MediaType{
+ Schema: &openapi3.SchemaRef{
+ Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.ReturnsType),
+ },
}
}
@@ -283,6 +285,9 @@ func (gen *generator) addField(schemaPropsV3 openapi3.Schemas, field *proto.Fiel
case googleMoneyType:
slog.Debug("Money", "name", fieldName, "type", fieldType, "format", fieldFormat)
gen.addGoogleMoneySchema()
+ case googleEmptyType:
+ slog.Debug("Empty", "name", fieldName, "type", fieldType, "format", fieldFormat)
+ gen.addGoogleEmptySchema()
default:
slog.Debug("Default", "name", fieldName, "type", fieldType, "format", fieldFormat)
}
@@ -318,6 +323,18 @@ func (gen *generator) addField(schemaPropsV3 openapi3.Schemas, field *proto.Fiel
}
}
+func (gen *generator) addGoogleEmptySchema() {
+ if _, ok := gen.openAPIV3.Components.Schemas[googleEmptyType]; ok {
+ return
+ }
+ gen.openAPIV3.Components.Schemas[googleEmptyType] = &openapi3.SchemaRef{
+ Value: &openapi3.Schema{
+ Description: "A generic empty message that you can re-use to avoid defining duplicated empty messages in your APIs. A typical example is to use it as the request or the response type of an API method. For instance:",
+ Type: "object",
+ },
+ }
+}
+
// addGoogleAnySchema adds a schema item for the google.protobuf.Any type.
func (gen *generator) addGoogleAnySchema() {
if _, ok := gen.openAPIV3.Components.Schemas[googleAnyType]; ok {
diff --git a/pb/generate.go b/pb/generate.go
index 0b75a6a..359c889 100644
--- a/pb/generate.go
+++ b/pb/generate.go
@@ -7,7 +7,7 @@ import (
func init() {}
//go:generate protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --twirp_out=. --twirp_opt=paths=source_relative xesite.proto
-//go:generate go run ../cmd/twirp-openapi-gen --in=xesite.proto --path-prefix=/api --servers=https://xeiaso.net --title=xeiaso.net --out=openapi.json
+//go:generate go run ../cmd/twirp-openapi-gen --verbose --in=xesite.proto --path-prefix=/api --servers=https://xeiaso.net --title=xeiaso.net --out=openapi.json
//go:embed xesite.proto openapi.json external/*.proto
var Proto embed.FS
diff --git a/pb/openapi.json b/pb/openapi.json
index c85f242..dc45d90 100644
--- a/pb/openapi.json
+++ b/pb/openapi.json
@@ -1,6 +1,10 @@
{
"components": {
"schemas": {
+ "google.protobuf.Empty": {
+ "description": "A generic empty message that you can re-use to avoid defining duplicated empty messages in your APIs. A typical example is to use it as the request or the response type of an API method. For instance:",
+ "type": "object"
+ },
"protofeed.Attachment": {
"description": "Attachment is an object representing a file associated with an item.",
"properties": {
@@ -221,7 +225,11 @@
"description": "\nGet fetches the current feed of posts.",
"requestBody": {
"content": {
- "application/json": {}
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/google.protobuf.Empty"
+ }
+ }
}
},
"responses": {
@@ -244,7 +252,11 @@
"description": "\nMetadata fetches the build metadata of the version of xesite that is\ncurrently running.",
"requestBody": {
"content": {
- "application/json": {}
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/google.protobuf.Empty"
+ }
+ }
}
},
"responses": {