Skip to content

OneOf support in OpenAPIv3 #251

@fenos

Description

@fenos

Hello there!
Thanks for the magical tool! It took a while to get my head around but it is great!

I was testing out the capability of the proto ->OpenAPIV3 and it works nicely.
However I noticed that it doesn't use the latest oneOf keywords to annotate a oneof type in the proto message.

For example given this proto file:

syntax = "proto3";

package twirp.example.haberdasher;

import "google/api/annotations.proto";
option go_package="twirp/example/haberdasher";


// Haberdasher service makes hats for clients.
service Haberdasher {
  // MakeHat produces a hat of mysterious, randomly-selected color!
  rpc MakeHat(Size) returns (Hat) {
    option(google.api.http) = {
      post: "/v1/create"
      body: "text"
    };
  };
}

// Size of a Hat, in inches.
message Size {
  int32 inches = 1; // must be > 0
}

// A Hat is a piece of headwear made by a Haberdasher.
message Hat {
  int32 inches = 1;
  string color = 2; // anything but "invisible"
  string name = 3; // i.e. "bowler"
  oneof type {
    Type1 type1 = 4;
    Type1 type2 = 5;
  }
}

message Type1 {
  string id = 1;
}

message Type2 {
  string id = 1;
}

I get this:

# Generated with protoc-gen-openapi
# https://github.com/googleapis/gnostic/tree/master/apps/protoc-gen-openapi

openapi: 3.0.3
info:
    title: Haberdasher
    description: Haberdasher service makes hats for clients.
    version: 0.0.1
paths:
    /v1/create:
        post:
            summary: MakeHat produces a hat of mysterious, randomly-selected color!
            operationId: Haberdasher_MakeHat
            parameters:
                - name: inches
                  in: query
                  schema:
                    type: string
            requestBody:
                content:
                    application/json: {}
                required: true
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Hat'
components:
    schemas:
        Hat:
            properties:
                inches:
                    type: integer
                    format: int32
                color:
                    type: string
                name:
                    type: string
                type1:
                    $ref: '#/components/schemas/Type1'
                type2:
                    $ref: '#/components/schemas/Type2'
            description: A Hat is a piece of headwear made by a Haberdasher.
        Type1:
            properties:
                id:
                    type: string
        Type2:
            properties:
                id:
                    type: string

note the type1 and type2 fields are flat in the schema instead of being something like:

type:
     oneOf:
                - $ref: '#/components/schemas/Type1'
                - $ref: '#/components/schemas/Type2'

Maybe I'm missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions