go-openapi / spec

openapi specification object model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Got '&errors.errorString{s:"child url is nil"}' when expanding spec.

pengsrc opened this issue · comments

Here's my swagger json files.

api_v2.0.json

{
  "swagger": "2.0",
  "info": {
    "version": "2016-01-01",
    "title": "Test",
    "license": {
      "name": "The Apache License (Version 2)",
      "url": "https://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "paths": {
    "/TestAction0": {
      "$ref": "./action.json#/TestAction0"
    },
    "/TestAction1": {
      "get": {
        "operationId": "TestAction1",
        "summary": "TestAction1",
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "object",
              "properties": {
                "action": {
                  "type": "string"
                },
                "ret_code": {
                  "type": "integer"
                },
                "data_set": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/data"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "data": {
      "type": "object",
      "properties": {
        "data_id": {
          "type": "string"
        },
        "data_name": {
          "type": "string"
        },
        "description": {
          "type": "string"
        }
      }
    }
  }
}

action.json

{
  "TestAction0": {
    "get": {
      "tags": [
        "Action"
      ],
      "operationId": "TestAction0",
      "summary": "TestAction0",
      "responses": {
        "200": {
          "description": "OK",
          "schema": {
            "type": "object",
            "properties": {
              "action": {
                "type": "string"
              },
              "ret_code": {
                "type": "integer"
              },
              "total_count": {
                "type": "integer"
              },
              "data_set": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/data"
                }
              }
            }
          }
        }
      }
    }
  }
}

I have also tried '"$ref": "http://127.0.0.1:9292/action.json#/TestAction0"', but same error.

Below is my test code.

package main

import (
    "testing"

    "github.com/go-openapi/loads"
    "github.com/stretchr/testify/assert"
)

func TestSpec(t *testing.T) {
    swagger, err := loads.Spec("/tmp/swagger/api_v2.0.json")
    assert.Nil(t, err)

    swaggerExpanded, err := swagger.Expanded()
    assert.Nil(t, err)
}

Got.

Error:      Expected nil, but got: &errors.errorString{s:"child url is nil"}

Hi, I submitted two pull request to fix this problem.


spec.ExpandOptions was added to provide configurations for expanding, currently there is only one option to use.

type ExpandOptions struct {
    RelativeBase string
}

RelativeBase is the base location to use when resolving relative references. If this value is not given, the working directory (os.Getwd()) will be used as RelativeBase.


Example:

package main

import (
    "testing"

    "github.com/go-openapi/loads"
    "github.com/stretchr/testify/assert"
)

func TestSpec(t *testing.T) {
    swagger, err := loads.Spec("/tmp/swagger/api_v2.0.json")
    assert.Nil(t, err)

    swaggerExpanded, err := swagger.Expanded(&spec.ExpandOptions{
        RelativeBase: "/tmp/swagger",
    })
    assert.Nil(t, err)
}