The code and data behind xeiaso.net
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/twirp-openapi-gen: fix empty schema

Signed-off-by: Xe Iaso <me@xeiaso.net>

Xe Iaso 5b8b30a3 9338a5b6

+58 -33
-1
.vscode/extensions.json
··· 1 1 { 2 2 "recommendations": [ 3 3 "unifiedjs.vscode-mdx", 4 - "golang.go", 5 4 "bbenoist.nix", 6 5 "jnoortheen.nix-ide", 7 6 "dhall.dhall-lang",
-3
cmd/twirp-openapi-gen/internal/generator/aliases.go
··· 74 74 Type: "boolean", 75 75 Format: "boolean", 76 76 }, 77 - "google.protobuf.Empty": { 78 - Type: "object", 79 - }, 80 77 81 78 "google.type.DateTime": { 82 79 Type: "string",
+43 -26
cmd/twirp-openapi-gen/internal/generator/handlers.go
··· 18 18 googleListValueType = "google.protobuf.ListValue" 19 19 googleStructType = "google.protobuf.Struct" 20 20 googleValueType = "google.protobuf.Value" 21 + googleEmptyType = "google.protobuf.Empty" 21 22 22 23 googleMoneyType = "google.type.Money" 23 24 ) ··· 92 93 var reqMediaType *openapi3.MediaType 93 94 switch rpc.RequestType { 94 95 case "google.protobuf.Empty": 95 - reqMediaType = openapi3.NewMediaType() 96 + gen.addGoogleEmptySchema() 96 97 default: 97 - if strings.Contains(rpc.RequestType, ".") { 98 - reqMediaType = &openapi3.MediaType{ 99 - Schema: &openapi3.SchemaRef{ 100 - Ref: fmt.Sprintf("#/components/schemas/%s", rpc.RequestType), 101 - }, 102 - } 103 - } else { 104 - reqMediaType = &openapi3.MediaType{ 105 - Schema: &openapi3.SchemaRef{ 106 - Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.RequestType), 107 - }, 108 - } 98 + } 99 + if strings.Contains(rpc.RequestType, ".") { 100 + reqMediaType = &openapi3.MediaType{ 101 + Schema: &openapi3.SchemaRef{ 102 + Ref: fmt.Sprintf("#/components/schemas/%s", rpc.RequestType), 103 + }, 104 + } 105 + } else { 106 + reqMediaType = &openapi3.MediaType{ 107 + Schema: &openapi3.SchemaRef{ 108 + Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.RequestType), 109 + }, 109 110 } 110 111 } 111 112 112 113 var resMediaType *openapi3.MediaType 113 114 switch rpc.ReturnsType { 114 115 case "google.protobuf.Empty": 115 - resMediaType = openapi3.NewMediaType() 116 + gen.addGoogleEmptySchema() 116 117 default: 117 - if strings.Contains(rpc.ReturnsType, ".") { 118 - resMediaType = &openapi3.MediaType{ 119 - Schema: &openapi3.SchemaRef{ 120 - Ref: fmt.Sprintf("#/components/schemas/%s", rpc.ReturnsType), 121 - }, 122 - } 123 - } else { 124 - resMediaType = &openapi3.MediaType{ 125 - Schema: &openapi3.SchemaRef{ 126 - Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.ReturnsType), 127 - }, 128 - } 118 + } 119 + 120 + if strings.Contains(rpc.ReturnsType, ".") { 121 + resMediaType = &openapi3.MediaType{ 122 + Schema: &openapi3.SchemaRef{ 123 + Ref: fmt.Sprintf("#/components/schemas/%s", rpc.ReturnsType), 124 + }, 125 + } 126 + } else { 127 + resMediaType = &openapi3.MediaType{ 128 + Schema: &openapi3.SchemaRef{ 129 + Ref: fmt.Sprintf("#/components/schemas/%s.%s", gen.packageName, rpc.ReturnsType), 130 + }, 129 131 } 130 132 } 131 133 ··· 283 285 case googleMoneyType: 284 286 slog.Debug("Money", "name", fieldName, "type", fieldType, "format", fieldFormat) 285 287 gen.addGoogleMoneySchema() 288 + case googleEmptyType: 289 + slog.Debug("Empty", "name", fieldName, "type", fieldType, "format", fieldFormat) 290 + gen.addGoogleEmptySchema() 286 291 default: 287 292 slog.Debug("Default", "name", fieldName, "type", fieldType, "format", fieldFormat) 288 293 } ··· 314 319 Type: "object", 315 320 }, 316 321 }, 322 + }, 323 + } 324 + } 325 + 326 + func (gen *generator) addGoogleEmptySchema() { 327 + if _, ok := gen.openAPIV3.Components.Schemas[googleEmptyType]; ok { 328 + return 329 + } 330 + gen.openAPIV3.Components.Schemas[googleEmptyType] = &openapi3.SchemaRef{ 331 + Value: &openapi3.Schema{ 332 + 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:", 333 + Type: "object", 317 334 }, 318 335 } 319 336 }
+1 -1
pb/generate.go
··· 7 7 func init() {} 8 8 9 9 //go:generate protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --twirp_out=. --twirp_opt=paths=source_relative xesite.proto 10 - //go:generate go run ../cmd/twirp-openapi-gen --in=xesite.proto --path-prefix=/api --servers=https://xeiaso.net --title=xeiaso.net --out=openapi.json 10 + //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 11 11 12 12 //go:embed xesite.proto openapi.json external/*.proto 13 13 var Proto embed.FS
+14 -2
pb/openapi.json
··· 1 1 { 2 2 "components": { 3 3 "schemas": { 4 + "google.protobuf.Empty": { 5 + "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:", 6 + "type": "object" 7 + }, 4 8 "protofeed.Attachment": { 5 9 "description": "Attachment is an object representing a file associated with an item.", 6 10 "properties": { ··· 221 225 "description": "\nGet fetches the current feed of posts.", 222 226 "requestBody": { 223 227 "content": { 224 - "application/json": {} 228 + "application/json": { 229 + "schema": { 230 + "$ref": "#/components/schemas/google.protobuf.Empty" 231 + } 232 + } 225 233 } 226 234 }, 227 235 "responses": { ··· 244 252 "description": "\nMetadata fetches the build metadata of the version of xesite that is\ncurrently running.", 245 253 "requestBody": { 246 254 "content": { 247 - "application/json": {} 255 + "application/json": { 256 + "schema": { 257 + "$ref": "#/components/schemas/google.protobuf.Empty" 258 + } 259 + } 248 260 } 249 261 }, 250 262 "responses": {