swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.98k stars 6.03k forks source link

$ref for local path not working #6223

Open djbusby opened 7 years ago

djbusby commented 7 years ago

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.

RobotMermaid commented 7 years ago

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/

vivek1082 commented 1 year ago

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 commented 1 year ago

@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.....

kusman28 commented 1 year ago

same problem here with @vivek1082

djbusby commented 1 year ago

@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.