nytimes/openapi2proto

Array $ref is ignored and instead uses a not-repeated type

JustinDFuller opened this issue · 0 comments

I am using a $ref to an array type in my swagger. It's not generating a repeated type. It only generates the repeated type if I inline the array $ref.

Here's an example.

swagger: "2.0"
info:
  description: "Problem With Arrays Example"
  version: "1.0.0"
  title: "Array Response"
host: "localhost:3000"
basePath: "/v1"
schemes:
- "https"
- "http"
paths:
  /cats:
    get:
      summary: "Get All Cats"
      produces:
      - "application/json"
      responses:
        200:
          description: OK
          schema:
            type: object
            properties:
              cats:
                $ref: "#/definitions/Cats"
definitions:
  Cat:
    type: object
    properties:
      name:
        type: string
      color:
        type: string
  Cats:
    type: array
    items:
      $ref: "#/definitions/Cat"                  

Here is the output. Note that cats in GetCatsResponse is not repeated.

syntax = "proto3";

package arrayresponse;

import "google/protobuf/empty.proto";

message Cat {
    string color = 1;
    string name = 2;
}

message GetCatsResponse {
    Cat cats = 1; // Notice that cats is not repeated - but it should be
}

service ArrayResponseService {
    // Get All Cats
    rpc GetCats(google.protobuf.Empty) returns (GetCatsResponse) {}
}

If I make a small adjustment to the api.yaml by inlining the array $ref, like this:

swagger: "2.0"
info:
  description: "Problem With Arrays Example"
  version: "1.0.0"
  title: "Array Response"
host: "localhost:3000"
basePath: "/v1"
schemes:
- "https"
- "http"
paths:
  /cats:
    get:
      summary: "Get All Cats"
      produces:
      - "application/json"
      responses:
        200:
          description: OK
          schema:
            type: object
            properties:
              cats:
                type: array
                items:
                  $ref: "#/definitions/Cat"    
definitions:
  Cat:
    type: object
    properties:
      name:
        type: string
      color:
        type: string

Now, cats is repeated in GetCatsResponse.

syntax = "proto3";

package arrayresponse;

import "google/protobuf/empty.proto";

message Cat {
    string color = 1;
    string name = 2;
}

message GetCatsResponse {
    repeated Cat cats = 1; // cats is now repeated
}

service ArrayResponseService {
    // Get All Cats
    rpc GetCats(google.protobuf.Empty) returns (GetCatsResponse) {}
}

Why is this happening? Swagger is interpreting it correctly in swagger UI. It seems like the $ref should produce the repeated type, just like the inline version.