question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Not resolve relative path to split files in generate command

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I have a trouble in file path on $ref property.

The following my open-api schema files split. index.yml is entry point.

api-schema/
├── api
│   └── v1
│       ├── posts.yml
├── index.yml
└── schemas
    └── post.yml

index.yml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    $ref: api/v1/posts.yml#/~1posts

api/v1/posts.yml

/posts:
  get:
    responses:
      '200':
        content:
          application/json:
            schema:
              $ref: './../../schemas/post.yml' # Here is problem!

I use two libraries below.

How to resolve path on $ref by them are not same.

api/v1/posts.yml

# Work on redoc. Redoc resolves current path as 'api-schema/api/v1/'.
#
# However not work on openapi-generator. It resolves current path as 'api-schema/'. This is where on index.yml.
# `openapi-generator validate -i api-schema/index.yml`
# Validate of openapi-generator was success.
$ref: './../../schemas/post.yml'

# Work on openapi-generator, but not work on redoc.
$ref: 'schemas/post.yml'

I don’t know which generator or redoc the bug for.

openapi-generator version

4.2.0

OpenAPI declaration file content or url

In my case, files are split.

Command line used for generation

When $ref: './../../schemas/post.yml', openapi-generator fails to generate and output the following.

[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./../../schemas/post.yml path: /Users/mitsuru/project/example-openapi
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:239)
	at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:119)
	at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:56)
	at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:202)
	at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:37)
	at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:52)
	at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:67)
	at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:83)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:66)
	at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:451)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:497)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:415)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)
Caused by: java.lang.RuntimeException: Could not find ./../../schemas/post.yml on the classpath
	at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:233)
	... 14 common frames omitted
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./../../schemas/post.yml
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./../../schemas/post.yml
Steps to reproduce

see description.

Related issues/PRs

Nothing.

Suggest a fix

I know whether this is bug.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mitsuru793commented, Sep 2, 2020

@nugrahawahyu

OK! I am not angry. I have thought to help you☺️ I am Japanese. I’m not good at speaking English. I am poor in expression of English🙏

$ref cannot be used directly under 200: (swagger-cli can handles it though)

Yes! I think it’s correct. I don’t know about openapi correcty😆

1reaction
nugrahawahyucommented, Sep 2, 2020

@mitsuru793 I’m still experiencing the issue (from master, using swagger-parser 2.0.20), care to elaborate how did you solve it?

My setup:

dir tree:

services/test/.
├── openapi.yaml
└── resources
    └── merchants
        ├── components
        │   ├── responses
        │   │   └── Merchants.yaml
        │   └── schemas
        │       ├── Merchant.yaml
        │       └── Merchants.yaml
        └── paths

openapi.yaml:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Marketplace
servers:
  - url: 'http://localhost:3039'
paths:
  /marketplace/merchants:
    $ref: 'resources/merchants/paths/actions.yaml'

resources/merchants/components/paths/actions.yaml:

get:
  summary: 'List'
  operationId: listMerchant
  tags:
    - merchant
  responses:
    200:
      $ref: '../components/responses/Merchants.yaml'

resources/merchants/components/responses/Merchant.yaml:

description: 'Merchant List'
content:
  application/json:
    schema:
      type: object
      properties:
        data:
          type: object
          properties:
            items:
              $ref: ../schemas/Merchants.yaml

resources/merchants/components/schemas/Merchant.yaml:

type: object
required:
  - address_id
  - name
properties:
  address_id:
    type: string
    format: uuid
  name:
    type: string
  description:
    type: string

resources/merchants/components/schemas/Merchants.yaml:

type: array
items:
  $ref: "Merchant.yaml"

Command line used for generation:

./mvnw clean install

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ 
  -i ~/test_project/api_schema/services/test/openapi.yaml \
  -g javascript \
  -o ~/test_project/client/test/javascript

Output:

[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
java.lang.RuntimeException: Unable to load RELATIVE ref: ../components/responses/Merchants.yaml path: /Users/user/test_project/api_schema/services/test
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:239)
	at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:119)
	at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalResponse(ExternalRefProcessor.java:344)
	at io.swagger.v3.parser.processors.ResponseProcessor.processReferenceResponse(ResponseProcessor.java:84)
	at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:41)
	at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:56)
	at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:84)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:67)
	at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:510)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:560)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:424)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Could not find ../components/responses/Merchants.yaml on the classpath
	at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
	at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:233)
	... 14 common frames omitted
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ../components/responses/Merchants.yaml
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 1, Warning count: 0
Errors:
	-Unable to load RELATIVE ref: ../components/responses/Merchants.yaml path: /Users/user/test_project/api_schema/services/test

	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:533)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:560)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:424)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert absolute path to relative path in PowerShell?
I found something built in, Resolve-Path: Resolve-Path -Relative. This returns the path relative to the current location. A simple usage:
Read more >
Relative and absolute paths, in the file system and on the web ...
Relative paths ​​ If you don't supply the root, it means that your path is relative. The simplest example of relative path is...
Read more >
How to Split Paths with the PowerShell Split-Path Cmdlet
Learn how to split and return different elements of a path using the PowerShell Split-Path cmdlet in this tutorial.
Read more >
Split-Path, Get-ChildItem – Splitting file names in PowerShell
The cmdlets Get-ChildItem and Split-Path will do the job. ... Produces an error because a relative path without -Resolve can't be resolved ...
Read more >
OperatingSystem - Robot Framework
File Should Not Exist can be used to avoid overwriting existing files. Create Directory. Arguments. path. Documentation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found