otter icon indicating copy to clipboard operation
otter copied to clipboard

[Bug]: openapi-generator 7.4.0 fails to resolve refs to files

Open fhamelin-1a opened this issue 6 months ago • 1 comments

Package name

schematics

Package version

11.0.1

Reproduction steps

You can ask me in Teams and I'll provide a reproduction .zip

Step 1: create models

OTTER_ROOT=todefine
mkdir ama-sdk-repro && cd ama-sdk-repro && mkdir models
cp $OTTER_ROOT/packages/@ama-sdk/showcase-sdk/openapi.yml ./models/pet-specs-untouched.yaml
cp ./models/pet-specs-untouched.yaml ./models/pet-specs-split.yaml
touch ./models/pet-response.yaml

Step 2: Manually modify pet-specs-split.yaml

Modify the paths./pet.post.responses.'200' to point to a components.response ok-pet:

  /pet:
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: Add a new pet to the store
      operationId: addPet
      responses:
        '200':
          $ref: '#/components/responses/ok-pet'

Create the components.response ok-pet:

  responses:
    ok-pet:
      description: Successful Pet operation
      content:
        application/xml:
          schema:
            $ref: './pet-response.yaml'
        application/json:
          schema:
            $ref: './pet-response.yaml'

In pet-response.yaml, modify the refs to point to pet-specs-split.yaml#/components. Also add a title:

x-swagger-router-model: io.swagger.petstore.model.Pet
title: Pet
type: object
required:
  - name
  - photoUrls
properties:
  id:
    type: integer
    format: int64
    example: 10
  name:
    type: string
    example: doggie
  category:
    $ref: './pet-specs-split.yaml#/components/schemas/Category'
  photoUrls:
    type: array
    xml:
      wrapped: true
    items:
      type: string
      xml:
        name: photoUrl
  tags:
    type: array
    xml:
      wrapped: true
    items:
      $ref: './pet-specs-split.yaml#/components/schemas/Tag'
      xml:
        name: tag
  status:
    type: string
    description: pet status in the store
    enum:
      - available
      - pending
      - sold
xml:
  name: pet

Step 3: generate SDKs for version 9.6.x and 11.0.x

npm create @[email protected] typescript sdk-otter-9 -- --spec-path ./models/pet-specs-untouched.yaml
npm create @ama-sdk typescript sdk-otter-11 -- --spec-path ./models/pet-specs-untouched.yaml

Note: I had troubles with --spec-path and --generator-key on v11 and had to manually modify the scripts in package.json.

Also, I've duplicated the spec:regen script so that it targets both split & untouched specs:

// v9
"spec:regen:untouched": "npm run generate -- --spec-path ../models/pet-specs-untouched.yaml && npm run clear-index",
"spec:regen:split": "npm run generate -- --spec-path ../models/pet-specs-split.yaml && npm run clear-index",

// v11
"spec:regen:untouched": "npm run generate -- --spec-path ../models/pet-specs-untouched.yaml --generator-key sdk-otter-11 && amasdk-clear-index",
"spec:regen:split": "npm run generate -- --spec-path ../models/pet-specs-split.yaml --generator-key sdk-otter-11 && amasdk-clear-index",

Current result

  • npm run spec:regen:untouched works for both v9 and v11
  • npm run spec:regen:split works for v9
  • npm run spec:regen:split fails for v11 with below exception
Did set selected version to 7.4.0
[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while resolving:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./pet-response.yaml path: /home/fhamelin/git_clones/ama-sdk-repro/sdk-otter-11
        at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:220)
        at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:150)
        at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:88)
        at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:236)
        at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:60)
        at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:56)
        at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:86)
        at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:88)
        at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:72)
        at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:59)
        at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:238)
        at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:181)
        at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:97)
        at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:686)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:744)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:527)
        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 ./pet-response.yaml on the classpath
        at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:33)
        at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:214)
        ... 18 common frames omitted
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./pet-response.yaml
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./pet-response.yaml
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./pet-response.yaml
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./pet-response.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: 2
Errors:
        -Unable to load RELATIVE ref: ./pet-response.yaml path: /home/fhamelin/git_clones/ama-sdk-repro/sdk-otter-11
Warnings:
        -Unable to load RELATIVE ref: ./pet-response.yaml path: /home/fhamelin/git_clones/ama-sdk-repro/sdk-otter-11

        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:717)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:744)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:527)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Error: OpenApiGeneratorCli failed to run OpenApiGenerator with command 'openapi-generator-cli generate --custom-generator=/home/fhamelin/git_clones/ama-sdk-repro/sdk-otter-11/node_modules/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/target/typescriptFetch-openapi-generator.jar -g typescriptFetch -i open-api.yaml -o .

Expected result

npm run spec:regen:split should work on v11 (and v10 which I haven't tried)

Additional comments

  • Using JDK 11.0.14.1-jbr
  • For the failing case, it uses openapi-generator 7.4.0. Tried to upgrade to openapi-generator 7.7.0 but same issue
  • The working case uses openapi-generator 6.6.0
  • When generating the typescriptFetch-openapi-generator.jar for 7.7.0, I had to modify the file AbstractTypeScriptClientCodegen because they moved the getSchemaItems method to ModelUtils (see DefaultCodegen.java). Note that the exception above has warnings for ModelUtils, so it could be related.
  • This kind of issue seems to be reccurrent with openapi-generator. It gets fixed then breaks again: https://github.com/OpenAPITools/openapi-generator/issues/17554 https://github.com/OpenAPITools/openapi-generator/issues/18429

fhamelin-1a avatar Jul 31 '24 09:07 fhamelin-1a