Skip to content

Commit 552e66e

Browse files
feat: set format for path param and add docs (#121)
* feat: set format for path param and add docs * test: add field format for path param case
1 parent 300cb9b commit 552e66e

File tree

7 files changed

+77
-3
lines changed

7 files changed

+77
-3
lines changed

README.md

+66
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protoc --oas_out=. service.proto
1818

1919
- support [API annotations](https://github.com/googleapis/googleapis/blob/master/google/api/annotations.proto) in methods
2020
- support [field behavior](https://github.com/googleapis/googleapis/blob/master/google/api/field_behavior.proto) in message field description
21+
- support [field info](https://github.com/googleapis/googleapis/blob/master/google/api/field_info.proto) in message field description
2122

2223
# Generate OpenAPI
2324

@@ -374,3 +375,68 @@ components:
374375
Empty:
375376
type: object
376377
```
378+
379+
## Set field format
380+
381+
```protobuf title="service.proto"
382+
syntax = "proto3";
383+
384+
package service.v1;
385+
386+
option go_package = "service/v1;service";
387+
388+
import "google/api/annotations.proto";
389+
import "google/api/field_info.proto";
390+
391+
service Service {
392+
rpc GetItem(GetItemRequest) returns (Item) {
393+
option (google.api.http) = {
394+
get: "/api/v1/items/{id}"
395+
};
396+
}
397+
}
398+
399+
message GetItemRequest {
400+
string id = 1 [(google.api.field_info).format = UUID4]; // <--
401+
}
402+
403+
message Item {
404+
string id = 1 [(google.api.field_info).format = UUID4]; // <--
405+
string name = 2;
406+
}
407+
```
408+
409+
```yaml title="openapi.yaml"
410+
openapi: 3.1.0
411+
info:
412+
title: ""
413+
version: ""
414+
paths:
415+
/api/v1/items/{id}:
416+
get:
417+
operationId: getItem
418+
parameters:
419+
- name: id
420+
in: path
421+
required: true
422+
schema:
423+
type: string
424+
format: uuid # <--
425+
responses:
426+
"200":
427+
description: service.v1.Service.GetItem response
428+
content:
429+
application/json:
430+
schema:
431+
$ref: '#/components/schemas/Item'
432+
components:
433+
schemas:
434+
Item:
435+
type: object
436+
properties:
437+
id:
438+
type: string
439+
format: uuid # <--
440+
name:
441+
type: string
442+
```

example/message.proto

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ message GetItemsResponse {
4141
}
4242

4343
message GetItemRequest {
44-
string id = 1 [(google.api.field_behavior) = REQUIRED];
44+
string id = 1 [
45+
(google.api.field_behavior) = REQUIRED,
46+
(google.api.field_info).format = UUID4
47+
];
4548
}
4649

4750
message UpdateItemRequest {

example/openapi.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ paths:
5050
required: true
5151
schema:
5252
type: string
53+
format: uuid
5354
responses:
5455
"200":
5556
description: service.v1.Service.GetItem response
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"openapi":"3.1.0","info":{"title":"","version":""},"paths":{"/api/v1/items/{id}":{"get":{"operationId":"getItem","parameters":[{"name":"id","in":"path","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"crud.v1.Crud.GetItem response","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Item"}}}}}}}},"components":{"schemas":{"Item":{"type":"object","properties":{"id":{"type":"string","format":"uuid"},"ipv4":{"type":"string","format":"ipv4"},"ipv6":{"type":"string","format":"ipv6"},"ip":{"type":"string","format":"ip"}},"required":["id","ipv4","ipv6","ip"]}}}}
1+
{"openapi":"3.1.0","info":{"title":"","version":""},"paths":{"/api/v1/items/{id}":{"get":{"operationId":"getItem","parameters":[{"name":"id","in":"path","required":true,"schema":{"type":"string","format":"uuid"}}],"responses":{"200":{"description":"crud.v1.Crud.GetItem response","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Item"}}}}}}}},"components":{"schemas":{"Item":{"type":"object","properties":{"id":{"type":"string","format":"uuid"},"ipv4":{"type":"string","format":"ipv4"},"ipv6":{"type":"string","format":"ipv6"},"ip":{"type":"string","format":"ip"}},"required":["id","ipv4","ipv6","ip"]}}}}

internal/gen/_golden/field_format.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ paths:
1212
required: true
1313
schema:
1414
type: string
15+
format: uuid
1516
responses:
1617
"200":
1718
description: crud.v1.Crud.GetItem response

internal/gen/_testdata/field_format.textproto

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ proto_file: {
5757
type: TYPE_STRING
5858
json_name: "id"
5959
options:{
60-
[google.api.field_behavior]:REQUIRED
60+
[google.api.field_behavior]:REQUIRED,
61+
[google.api.field_info]:{format:UUID4}
6162
}
6263
}
6364
}

internal/gen/generator.go

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ func (g *Generator) mkParameter(in, name string, f *protogen.Field) (*ogen.Param
354354
return nil, errors.Wrapf(err, "generate %s parameter %q", in, f.Desc.Name())
355355
}
356356

357+
setFieldFormat(s, f.Desc.Options())
358+
357359
p := ogen.NewParameter().
358360
SetIn(in).
359361
SetName(name).

0 commit comments

Comments
 (0)