go-openapi / spec

openapi specification object model
Apache License 2.0
389 stars 98 forks source link

Spec parser ignores response entries when there's an extension #165

Closed zmay2030 closed 1 year ago

zmay2030 commented 1 year ago

Problem statement

According to the OpenAPI Spec, OpenAPI Extensions are allowed to be under responses. But when loading the spec via load.Spec(), the response codes are completely ignored if there is an extension. If there is no extension, then the response codes are parsed and hydrated just fine.

OpenAPI specification

OpenAPI Extensions

Steps to reproduce

Spec file (spec.json)

{
  "swagger": "2.0",
  "info": {
    "title": "Swagger Fixture",
    "version": "1.0",
    "x-ext-info-1": "a",
    "x-ext-info-2": "b"
  },
  "paths": {
    "/b/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response"
          },
          "x-ext-resp": "response ext"
        }
      }
    }
  }
}

Code:

package main

import (
    "github.com/davecgh/go-spew/spew"
    "github.com/go-openapi/loads"
)

func main() {
    storedSpec, err := loads.Spec("./spec.json")
    if err != nil {
        panic(err)
    }

    spew.Dump(storedSpec.Spec().Paths.Paths["/b/"].Get.Responses)
}

Output:

aven30@COMP archive % go run main.go
(*spec.Responses)(0x140001a4df8)({
 VendorExtensible: (spec.VendorExtensible) {
  Extensions: (spec.Extensions) (len=1) {
   (string) (len=10) "x-ext-resp": (string) (len=12) "response ext"
  }
 },
 ResponsesProps: (spec.ResponsesProps) {
  Default: (*spec.Response)(<nil>),
  StatusCodeResponses: (map[int]spec.Response) <nil>
 }
})

But when you remove the "x-ext-resp": "response ext", it ends up dumping the following:

aven30@COMP archive % go run main.go
(*spec.Responses)(0x140000a2df8)({
 VendorExtensible: (spec.VendorExtensible) {
  Extensions: (spec.Extensions) <nil>
 },
 ResponsesProps: (spec.ResponsesProps) {
  Default: (*spec.Response)(<nil>),
  StatusCodeResponses: (map[int]spec.Response) (len=1) {
   (int) 200: (spec.Response) {
    Refable: (spec.Refable) {
     Ref: (spec.Ref) 
    },
    ResponseProps: (spec.ResponseProps) {
     Description: (string) (len=12) "200 response",
     Schema: (*spec.Schema)(<nil>),
     Headers: (map[string]spec.Header) <nil>,
     Examples: (map[string]interface {}) <nil>
    },
    VendorExtensible: (spec.VendorExtensible) {
     Extensions: (spec.Extensions) <nil>
    }
   }
  }
 }
})

Environment

swagger version:latest dev go version: 1.19.1 OS: Mac