Breaking swagger spec into multiple files with $ref causes nil pointer dereference.
chris-langager opened this issue · comments
Chris Langager commented
We are attempting to break a large swagger file into multiple files for ease of management. Here's a simple example that replicates this issue:
splitSwaggerSpec.json:
{
"swagger": "2.0",
"info": {
"title": "Testing out splitting up swagger spec into multiple files",
"version": "v1"
},
"paths": {
},
"definitions": {
"user": {
"$ref": "./splitSwaggerSpecUserDefinition.json"
}
}
}
splitSwaggerSpecUserDefinition.json:
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
spec, err := loads.Spec("./splitSwaggerSpec.json")
// spec.Spec().Definitions["user"].SchemaProps.Ref is set to ./splitSwaggerSpecUserDefinition.json, however no other SchemaProps are set
//attempting to Expand the spec results in a panic
expandedSpec , err = spec.Expanded()
This is the panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x695d1]
goroutine 1 [running]:
github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/spec.expandSchema(0x0, 0x0, 0x0, 0xc8202c4380, 0x0, 0x0, 0x0, 0x100, 0x0, 0x0, ...)
/Users/chris.langager/go/src/github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/spec/expander.go:442 +0x4f1
github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/spec.ExpandSpec(0xc820327300, 0x0, 0x0)
/Users/chris.langager/go/src/github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/spec/expander.go:325 +0x28a
github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/loads.(*Document).Expanded(0xc8203a7840, 0x1, 0x0, 0x0)
/Users/chris.langager/go/src/github.com/khelldar/SwaggerToServer/vendor/github.com/go-openapi/loads/spec.go:140 +0xe1
main.main()
/Users/chris.langager/go/src/github.com/khelldar/SwaggerToServer/main.go:94 +0x1c5
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1721 +0x1
Is it possible to expand references that are across different .json or .yaml files? If not, I would be willing to add this as a feature, provided you are accepting pull requests on this project.
Ivan Porto Carrero commented
I would be willing to add this, but it is against the ref spec IIRC
we use something like this in a pre-generate step
#!/bin/bash
# Background
# go-swagger doesn't handle external definition reference well in generated
# go code. It doesn't generate anything regarding the models which causes
# problems if the models are generated in a separated package.
# The solution is to merge the models into swagger file then go-swagger is able
# to generate the reference to the models with right package name.
# Before go-swagger handles well with JSON references, this script is a
# workaround to make our swagger spec look better.
SPEC_DIR="$1"
API_SPEC="$2"
API_SPEC_FILE="$SPEC_DIR/$API_SPEC-swagger.yml"
MODEL_SPEC_FILE="$SPEC_DIR/models-swagger.yml"
combine() {
cat "$API_SPEC_FILE"
if grep 'definitions:' "$API_SPEC_FILE" >/dev/null 2>&1 ; then
grep -v 'swagger:' "$MODEL_SPEC_FILE" | grep -v 'definitions:'
else
grep -v 'swagger:' "$MODEL_SPEC_FILE"
fi
}
combine | sed 's#./models-swagger.yml##g' >$API_SPEC-swagger.gen.yml
Ivan Porto Carrero commented
I do think it would be helpful to have especially because refs do support http urls