openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open mitsuru793 opened this issue 7 years ago • 12 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] 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.

  • https://github.com/OpenAPITools/openapi-generator
  • https://github.com/Redocly/redoc

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.

mitsuru793 avatar Nov 28 '19 12:11 mitsuru793

I am having the exact same issue. Did you find any fixes @mitsuru793 ?

RaduFurnea avatar Apr 14 '20 10:04 RaduFurnea

Any news? Is this the desired behavior, therefore it would not be a bug?

jorgerod avatar Apr 15 '20 10:04 jorgerod

@RaduFurnea @jorgerod

I use swagger-cli to bundle them and then use openapi-generator. https://www.npmjs.com/package/swagger-cli

I don't know other solving.

mitsuru793 avatar Apr 15 '20 13:04 mitsuru793

Check this issue, there is also proposed fix, so it might help you: https://github.com/OpenAPITools/openapi-generator/issues/3848

ptopor avatar May 13 '20 08:05 ptopor

File/folder structure:

├── README.md
├── components
│   ├── schemas
│   │   └── mainType
│   │       └── subType.yml
│   └── mainTypeUrl.yml
├── openapi.yml

openapi.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: components/mainTypeUrl.yml/#/~1sometype

components/mainTypeUrl.yml

/sometype:
  get:
    responses:
      "200":
        $ref: "schemas/mainType/subType.yml" # The issue

Error:

[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
40 java.lang.RuntimeException: Unable to load RELATIVE ref: schemas/mainType/subType.yml path: /some/path/to/root

It's quite annoying as we would like to split parts of the spec

xvilo avatar Jun 19 '20 14:06 xvilo

@ptopor Thank you for replying. I have confirmed that it has solved. I am happy!

mitsuru793 avatar Aug 19 '20 13:08 mitsuru793

@xvilo I think the components/mainTypeUrl.yml requires content property under responses. document: https://swagger.io/docs/specification/describing-responses/

mitsuru793 avatar Aug 19 '20 13:08 mitsuru793

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

nugrahawahyu avatar Sep 02 '20 04:09 nugrahawahyu

@nugrahawahyu Hi!

There are lots of typo of path in your reply. I have a trouble. I think you are beginner. Me too.

I’ll give you advices about openapi-generator. I had a lot of trouble and solve them.

  1. Log of openapi is not easy to read. I don’t know Java. So, we have to build command frequently, but when also small change.
  2. There is a openapi lint tool. I am going to use it. Then, the following company has made very excellent openapi editor.
https://meta.stoplight.io/docs/spectral/README.md
  3. Openapi-generator is not equal to openapi. Generator is a third party library. We have to be care bugs by whether openapi rule or generator.

————

I have copied the codes you wrote to the following repository and fix correctly. https://github.com/mitsuru793/issues/blob/master/openapi-generator-relative-path-other-question/README.md

Please see at scripts in package.json. You have to run yarn run build.

The following are reasons of the problem.

  1. You have some typo paths.
  2. You don’t know format of response. Please read document.
    1. https://swagger.io/docs/specification/describing-request-body/
    2. https://swagger.io/docs/specification/media-types/
responses:
  200:
    description: success response
    content:
      application/json: // need
        schema:         // need
          type: object  // need
          properties:   // need
            marchants:  // need
              $ref: '../components/schemas/Merchants.yaml'

mitsuru793 avatar Sep 02 '20 14:09 mitsuru793

@mitsuru793, My bad, sorry for typos and the incomplete example (resources/merchants/components/responses/Merchant.yaml, I've updated it). In the updated example, the issue is:

200:
      $ref: '../components/responses/Merchants.yaml'

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

Thank you for your example, especially the linter, truly appreciate it!

nugrahawahyu avatar Sep 02 '20 16:09 nugrahawahyu

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

mitsuru793 avatar Sep 02 '20 16:09 mitsuru793

This issue has resolved. I'll close it.

mitsuru793 avatar Sep 02 '20 17:09 mitsuru793