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

[BUG] references to external files does not work

Open YishTish opened this issue 7 years ago • 11 comments

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

https://github.com/OpenAPITools/openapi-generator/issues/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.

YishTish avatar Jun 27 '19 11:06 YishTish

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.

jmini avatar Jun 28 '19 06:06 jmini

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'

DonDi94 avatar Jun 28 '19 08:06 DonDi94

@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 avatar Jun 28 '19 08:06 jmini

I have started https://github.com/OpenAPITools/openapi-generator/pull/3239

jmini avatar Jun 28 '19 09:06 jmini

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)

YishTish avatar Jul 02 '19 07:07 YishTish

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

jmini avatar Jul 07 '19 13:07 jmini

@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" : "{}"
  }
},

DonDi94 avatar Jul 10 '19 11:07 DonDi94

@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 https://github.com/swagger-api/swagger-parser/issues/1147 with your example.

jmini avatar Jul 10 '19 18:07 jmini

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

DonDi94 avatar Jul 10 '19 21:07 DonDi94

https://github.com/swagger-api/swagger-parser/issues/1147 seems to be fixed in swagger-parser and we did an update in OpenAPI-Generator to use this new version (see https://github.com/OpenAPITools/openapi-generator/pull/3737)

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

jmini avatar Aug 24 '19 08:08 jmini

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.

DonDi94 avatar Aug 26 '19 08:08 DonDi94