Skip to content

error when parsing relative $ref #1865

@hauner

Description

@hauner

Hi,

I have an OpenAPI description with relative $refs that fails to parse with any version larger than io.swagger.parser.v3:swagger-parser:2.0.28.

It worked until 2.0.28.

See below for the OpenAPI yaml files and the exception.

I have created a test-case that throws the below exception.

broken-ref/apis/foo.yaml
broken-ref/examples/foo.example.yaml
broken-ref/models/foo.model.yaml
broken-ref/openapi30.yaml
#broken-ref/apis/foo.yaml
openapi: 3.0.0
info:
  title: swagger parser bug, worked until 2.0.28 but not since 2.0.29
  version: '1.0'

paths:

  '/foo':
    post:
      summary: create foo
      description: Create a new foo.
      responses:
        '200':
          description: a Foo
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
              examples:
                Foo Bar:
                  $ref: '#/components/examples/FooExample'
      requestBody:
        description: foo data
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Foo'
            examples:
              Foo Bar:
                $ref: '#/components/examples/FooExample'

components:
  schemas:
    Foo:
      $ref: ../models/foo.model.yaml
  examples:
    FooExample:
      $ref: ../examples/foo.example.yaml
# broken-ref/examples/foo.example.yaml
value:
  foo: bar
#broken-ref/models/foo.model.yaml
title: Foo
type: object
properties:
  foo:
    type: string
# broken-ref/openapi30.yaml
openapi: 3.0.3
info:
  title: ref chain
  version: '1'

paths:
  /foo:
    $ref: ./apis/foo.yaml#/paths/~1foo
[main] WARN io.swagger.v3.parser.OpenAPIV3Parser - Exception while resolving:
java.lang.RuntimeException: Unable to load RELATIVE ref: foo.example path: /Users/hauner/Development/openapi/swagger-parser/modules/swagger-parser-v3/target/test-classes/broken-ref
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:216)
	at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:150)
	at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalExample(ExternalRefProcessor.java:691)
	at io.swagger.v3.parser.processors.ExampleProcessor.processReferenceExample(ExampleProcessor.java:45)
	at io.swagger.v3.parser.processors.ExampleProcessor.processExample(ExampleProcessor.java:30)
	at io.swagger.v3.parser.processors.ComponentsProcessor.processExamples(ComponentsProcessor.java:183)
	at io.swagger.v3.parser.processors.ComponentsProcessor.processComponents(ComponentsProcessor.java:111)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:73)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:59)
	at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:226)
	at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:178)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:94)
	at io.swagger.v3.parser.test.BrokenRefTest.testBrokenRef(BrokenRefTest.java:18)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
	at org.testng.TestRunner.privateRun(TestRunner.java:782)
	at org.testng.TestRunner.run(TestRunner.java:632)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
	at org.testng.SuiteRunner.run(SuiteRunner.java:268)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.run(TestNG.java:1064)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.RuntimeException: Could not find foo.example on the classpath
	at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:210)
	... 35 more

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions