Open jan-sykora opened 2 years ago
Hi @jan-sykora, it would make a lot of sense to have this.
I would even say that it should probably be default behaviour to add nullable
for optional
fields, since it's a non-breaking change that adds valuable information for both humans and machines.
I have noticed a potential inconsistency regarding this mapping. When the optional proto field is a message, it is converted into OpenAPI as a schema component that is then referenced.
The problem is when there's a protofile like this:
message Foo {
optional google.protobuf.Duration optional_bar_duration = 1;
google.protobuf.Duration bar_duration = 1;
}
message google.protobuf.Duration {
int64 seconds = 1;
int32 nanos = 2;
}
then it's generated as
components:
schemas:
Foo:
properties:
optionalBarDuration:
$ref: '#/components/schemas/Duration'
barDuration:
$ref: '#/components/schemas/Duration'
Duration:
type: object
properties:
seconds:
type: integer
format: int64
nanos:
type: integer
format: int32
-> we cannot mark the whole Duration object as nullable because then it would be applied for both optional_bar_duration and bar_duration (but we want it to be applied only on optional_bar_duration).
Also created related issue #351.
Good catch :) I agree that this should be a string. We should look into how we can handle optional message types best.
@morphar / @jan-sykora the optional
protocol buffer option doesn't actually make the field nullable. To make a nullable int32 (or any other primitive type nullable) you need to use one of the wrapper types.
nullable: true
property to the output. We can probably update that code to spit out nullable.Maybe fix this along with #351 as they are sort of related to the well known types.
Would it be possible to add an option to protoc-gen-openapi to generate proto3 fields labeled as
optional
to OpenAPI object properties with labelnullable: true
?For example this proto file:
when generated by protoc-gen-openapi with some new option (e.g. proto3-optional-nullable=true) would be generated as OpenAPI spec:
If I understand it correctly:
optional
labelnil
value represents whether the field was explicitly setnull
value to the object property. But to make it work, the property must be labeled as nullable.The similar feature is supported in grpc-gateway/protoc-gen-openapiv2 - it supports option to generate object properties in OAS2 as nullable -> the main motivation for this option is that some other tools generating code from the (generated) OpenAPI spec cannot handle default (null) values set during the transition from proto3 marshaled wire format to openapi json.
This issue is related to #275.