Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] references to external files does not work #3233

Closed
YishTish opened this issue Jun 27, 2019 · 11 comments
Closed

[BUG] references to external files does not work #3233

YishTish opened this issue Jun 27, 2019 · 11 comments

Comments

@YishTish
Copy link
Contributor

YishTish commented Jun 27, 2019

Description

Generating code with references to external files does not work (used to work in V2).

stack trace (last lines):

> [main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./event.json#/schemas/event
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:
	-null

	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:606)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:395)
	at 

> org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)

openapi-generator version

4.0.2

OpenAPI declaration file content or url

https://gist.github.com/YishTish/d2f8335e6027f4d50beec84abdf20b90

Command line used for generation

regular bash: openapi-generator generate -i openapi.json -g openapi-yaml -o generated/

Steps to reproduce

Just run the code with the jsons specified.

Related issues/PRs

#3026

Suggest a fix

When $ref starts with './', change behaviour to pick up the data from external file. Note that the file location is always relative, and absolute path can (should?) be considered wrong.

@jmini jmini changed the title [BUG] Description [BUG] references to external files does not work Jun 28, 2019
@jmini
Copy link
Member

jmini commented Jun 28, 2019

I fear that this is an issue with Swagger-Parser.

Since your example is quite big, it take some times to understand which part is broken.

@DonDi94
Copy link
Contributor

DonDi94 commented Jun 28, 2019

I encountered the same problem and it is indeed something about Swagger-Parser.

To be more precise, I have had this problem when 1) defining content with reference schema for parameters and 2) referencing a requestBody component that has an external schema reference which also references another schema.

#test.yaml
openapi: 3.0.0
info:
    version: "1.0"
    title: Swagger Parser Issue
paths:
  /:
    post:
      parameters:
        - name: param
          in: query
          required: true
          content:
            'application/json':
              schema:
                $ref: 'common.yaml#/components/schemas/ExternalSchema'
      requestBody:
        $ref: '#/components/requestBodies/RefRequestBody'
      responses:
        default:
          description: "Error"
components:
  requestBodies:
    RefRequestBody:
      required: true
      content:
        'application/json':
          schema:
            $ref: 'common.yaml#/components/schemas/ExternalSchema'

#common.yaml                          
openapi: 3.0.0
info:
    version: "1.0"
    title: Swagger Parser Issue
paths: {}
components:
  schemas:
    AnotherSchema:
      type: string
    ExternalSchema:
      type: object
      properties:
        val:
          $ref: '#/components/schemas/AnotherSchema'

@jmini
Copy link
Member

jmini commented Jun 28, 2019

@DonDi94:

We are using:

    <dependency>
        <groupId>org.openapitools.swagger.parser</groupId>
        <artifactId>swagger-parser</artifactId>
        <version>2.0.13-OpenAPITools.org-1</version>
    </dependency> 

and swagger-parser did publish an update recently.
I can not reproduce the problem anymore with:

    <dependency>
        <groupId>io.swagger.parser.v3</groupId>
        <artifactId>swagger-parser</artifactId>
        <version>2.0.13</version>
    </dependency>

Test program:

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;

public class SwaggerParserMain {

	public static void main(String[] args) throws Exception {
		final String inputSpec = "<your file>";

		OpenAPIParser openApiParser = new OpenAPIParser();
		ParseOptions options = new ParseOptions();
		options.setResolve(true);
		options.setFlatten(true);

		OpenAPI openAPI = openApiParser.readLocation(inputSpec, null, options).getOpenAPI();
		String string = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);

		System.out.println(string);
	}
}

We discussed already that we would like to update. I will see if we can do a 2.0.13-OpenAPITools.org-2 release.

@YishTish: I did not look, but I hope your problem is the same.

@jmini
Copy link
Member

jmini commented Jun 28, 2019

I have started #3239

@YishTish
Copy link
Contributor Author

YishTish commented Jul 2, 2019

I've made some changes to the files in the gist. Apparently, there were some schema issues with them that I didn't realize.
The updated jsons are valid. I found an npm package (json-schema-ref-parser) that replaces the $refs with the data that is referenced, and that worked (got a python-flask project generated)

@jmini
Copy link
Member

jmini commented Jul 7, 2019

@YishTish & @DonDi94: we have updated the Swagger-Parser version we are using. Can you retry your cases with a recent 4.0.3-SNAPSHOT version?
(it should now work with the $refs as discussed, no need to replace them before using OpenAPI-Generator)

@DonDi94
Copy link
Contributor

DonDi94 commented Jul 10, 2019

@jmini for my use now it works fine, the referenced schemes for parameters with content are referenced correctly. I do have a bug to report though, as this case that I was using for testing is still not working.

#test.yaml
openapi: 3.0.0
info:
    version: "1.0"
    title: Swagger Parser Issue
paths:
  /:
    post:
      requestBody:
        $ref: '#/components/requestBodies/RefRequestBody'
      responses:
        default:
          description: "Error"
components:
  requestBodies:
    RefRequestBody:
      content:
        'application/json':
          schema:
            $ref: 'common.yaml#/components/schemas/StringObject'

With the external reference pointing to

#common.yaml                          
openapi: 3.0.0
info:
    version: "1.0"
    title: Swagger Parser Issue
paths: {}
components:
  schemas:
    String:
      type: string
    StringObject:
      type: object
      properties:
        val:
          $ref: '#/components/schemas/String'

This example results in StringObject failing to get the reference for String

"StringObject" : {
  "type" : "object",
  "properties" : {
    "val" : {
      "$ref" : "./common.yaml#/components/schemas/String"
    }
  },
  "example" : {
    "val" : "{}"
  }
},

@jmini
Copy link
Member

jmini commented Jul 10, 2019

@DonDi94: thank you a lot for your example.

IMO this is definitively an error produced by Swagger-Parser (that merges for us all separated files into a single one). I have raised swagger-api/swagger-parser#1147 with your example.

@DonDi94
Copy link
Contributor

DonDi94 commented Jul 10, 2019

@jmini no problem I'm glad to help! Yes I agree, the previously reported problem was for the exact same issue in referenced schemes in parameter's contents. In that case I was getting the same result with the .common.yaml/... in the processed JSON for parameters.

@jmini
Copy link
Member

jmini commented Aug 24, 2019

swagger-api/swagger-parser#1147 seems to be fixed in swagger-parser and we did an update in OpenAPI-Generator to use this new version (see #3737)

@DonDi94: Can you try with the latest 4.1.1-SNAPSHOT ?

@DonDi94
Copy link
Contributor

DonDi94 commented Aug 26, 2019

swagger-api/swagger-parser#1147 seems to be fixed in swagger-parser and we did an update in OpenAPI-Generator to use this new version (see #3737)

@DonDi94: Can you try with the latest 4.1.1-SNAPSHOT ?

I tested on the previous case and it works, all the references are resolved correctly.

@jmini jmini closed this as completed Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants