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]: openapi-generator 7.4.0 fails to resolve refs to files #2014

Closed
fhamelin-1a opened this issue Jul 31, 2024 · 1 comment · Fixed by #2046
Closed

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

fhamelin-1a opened this issue Jul 31, 2024 · 1 comment · Fixed by #2046
Labels
bug Something isn't working priority:high

Comments

@fhamelin-1a
Copy link

fhamelin-1a commented Jul 31, 2024

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 @ama-sdk@9.6.3 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

@fhamelin-1a fhamelin-1a added bug Something isn't working triage labels Jul 31, 2024
@fhamelin-1a fhamelin-1a changed the title [Bug]: openapitools 7.4.0 fails to resolve refs to files [Bug]: openapi-generator 7.4.0 fails to resolve refs to files Jul 31, 2024
@fpaul-1A
Copy link
Contributor

fpaul-1A commented Aug 7, 2024

This seems to be a regression linked to #1572
We are now copying the yaml file before sending it to openapi generator which breaks all the relative paths
Placing the referenced files at the root of the generated sdks make it works (copy ama-sdk-repro/models/pet-response.yaml into ama-sdk-repro/sdk-otter-11/pet-response.yaml)

We need to discuss to find a proper fix

github-merge-queue bot pushed a commit that referenced this issue Aug 23, 2024
## Proposed change

When we copy the spec file before generating the SDK we are breaking the
local relative paths referenced in the file.
The proposed change is to copy all the referenced files inside the SDK
in a folder `./spec-local-references/` to have them versioned.
We also update the local references in the spec to look for these files.

## Related issues

- 🐛 Fixes #2014
- 🚀 Feature #(issue)

<!-- Please make sure to follow the contributing guidelines on
https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
cpaulve-1A added a commit that referenced this issue Aug 27, 2024
## Proposed change

Cherry Pick fix of #2046

## Related issues

- 🐛 Fixes #2014 

<!-- Please make sure to follow the contributing guidelines on
https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants