swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
777 stars 525 forks source link

swagger-parser not resolving file references #963

Open jshort opened 5 years ago

jshort commented 5 years ago

Dependencies:

161       <dependency>
162         <groupId>io.swagger</groupId>
163         <artifactId>swagger-compat-spec-parser</artifactId>
164         <version>1.0.40</version>
165       </dependency>
166       <dependency>
167         <groupId>io.swagger</groupId>
168         <artifactId>swagger-models</artifactId>
169         <version>1.5.21</version>
170       </dependency>
171       <dependency>
172         <groupId>io.swagger</groupId>
173         <artifactId>swagger-parser</artifactId>
174         <version>1.0.40</version>
175       </dependency>

Excerpt from main.yaml yaml spec:

definitions:
  $ref: './definitions.yaml'

definitions.yaml is in the same directory as main.yaml and looks like:

ErrorResponse:
  type: object
  required:
    - code
    - message
  properties:
    code:
      description: The HTTP error status code
      type: integer
      format: int32
    message:
      description: The reason for the error
      type: string
    fields:
      description: An array of strings for specific reasons for the error
      type: array
      items:
        type: string

I have some simple code that parses the spec and the messages indicates my definitions ref is not good and the swagger object has no definitions:

319             SwaggerDeserializationResult dsr = new SwaggerParser().readWithInfo(path, null, true);
320             
321             dsr.getMessages().forEach(e -> LOG.info("Message: " + e));
322             spec = dsr.getSwagger();
323             spec.getPaths().forEach((k,v) -> LOG.info("Key: " + k + " and Value: " + v));
324             spec.getDefinitions().forEach((k,v) -> LOG.info("Key: " + k + " and Value: " + v));

Messages:

[INFO] Message: attribute definitions.$ref is not of type `object`
jshort commented 5 years ago

So this does work:

definitions:
  Message:
    $ref: './definitions.yaml#/Message'
  ErrorResponse:
    $ref: './definitions.yaml#/ErrorResponse'

However I've seen multiple swagger 2.0 examples showing that you can set paths and definitions at the root level. What has changed?