swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

$ref for local path not working

Open djbusby opened this issue 8 years ago • 5 comments

I have a swagger.yaml file that is getting huge, 10K+ lines, so it makes sense to break this into logical pieces. Documentation suggests I use '$ref' for this purpose.

I tried this:

swagger: '2.0'
info:
  $ref: './swagger/info.yaml'

Followed by:

java -jar swagger-codegen-cli.jar \
	generate \
	--input-spec doc/swagger.yaml \
	--lang html \
	--output ./webroot/swagger/

Rather than including the contents of the ./swagger/info.yaml file, we get this:

[main] INFO io.swagger.parser.Swagger20Parser - reading from doc/swagger.yaml
[main] WARN io.swagger.codegen.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.swager-codegen-ignore) will be evaluated.
[main] ERROR io.swagger.codegen.DefaultGenerator - Missing required field info version. Default appVersion set to 1.0.0
[main] ERROR io.swagger.codegen.DefaultGenerator - Missing required field info version. Default version set to 1.0.0
Exception in thread "main" java.lang.NullPointerException
	at io.swagger.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:759)
	at io.swagger.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:388)
	at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:700)
	at io.swagger.codegen.cmd.Generate.run(Generate.java:285)
	at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)

It's similar to this issue maybe? https://github.com/swagger-api/swagger-editor/issues/233

Since I have to represent almost 100 paths, I was hoping to have a paths section that looks like:

paths:
  /magic-one:
    $ref: './swagger/magic-one.yaml'
  ./magic-two:
    $ref: './swagger/magic-two.yaml'

As it stands now, even when $ref resolves information in the same file (like Models) my paths section is too dang tall; the file is getting hard to manage.

Coming from asciidoc background I miss the include::path/file.ad[] option.

djbusby avatar Aug 02 '17 21:08 djbusby

having the same problem and found this:

Places Where $ref Can Be Used

A common misconception is that $ref is allowed anywhere in an OpenAPI specification file. Actually $ref is only allowed in places where the OpenAPI 3.0 Specification explicitly states that the value may be a reference.

For example, $ref cannot be used in the info section and directly under paths:

openapi: 3.0.0

Incorrect!

info: $ref: info.yaml paths: $ref: paths.yaml

in: https://swagger.io/docs/specification/using-ref/

RobotMermaid avatar Aug 23 '17 22:08 RobotMermaid

Individual path reference allowed according to document

However, you can $ref individual paths, like so: paths: /users: $ref: '../resources/users.yaml' /users/{userId}: $ref: '../resources/users-by-id.yaml'

vivek1082 avatar Jan 18 '23 14:01 vivek1082

@djbusby did you get solution, how did u solve this. I am getting same error. Considering this thread is 5 year old, any memory brush up u can do.....

vivek1082 avatar Jan 18 '23 14:01 vivek1082

same problem here with @vivek1082

kusman28 avatar Sep 15 '23 15:09 kusman28

@vivek1082 and @kusman28 here is where some code I made sorta-solves this: https://github.com/openthc/api/blob/master/bin/build-openapi.php#L54

That script takes the source with the '$ref', then recursively tries to resolve them to build a single output file. Then that output file is fed to code-gen.

djbusby avatar Sep 15 '23 16:09 djbusby