cli icon indicating copy to clipboard operation
cli copied to clipboard

fix: generating fromTemplate with refs in spec

Open Laupetin opened this issue 2 years ago • 20 comments

Description

Updates the code for generation via template to add the path of the specification file to the options passed to the generator. This makes the generator able to resolve paths specified in $ref properties.

Related issue(s) Fixes #944

Laupetin avatar Dec 01 '23 11:12 Laupetin

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar Dec 01 '23 11:12 sonarqubecloud[bot]

@Laupetin hey, thanks for the PR.

I just had a look at related issue and started digging a bit as it was confusing why it works in Generator CLI (next time please specify version that you use, it is super important) and do not work in AsyncAPI CLI.

Please have a look at https://github.com/asyncapi/parser-js/issues/797#issuecomment-1618966765 I have a feeling this PR workarounds this underlying problem

derberg avatar Dec 18 '23 10:12 derberg

@derberg Hi, thanks for checking it out. Sorry for the delayed answer.

I can reproduce this issue with @asyncapi/cli/1.2.35 and it works with @asyncapi/generator 1.16.0.

From my understanding the root of this issue is that the CLI first loads the spec from the file to a string in-memory. Then it calls the parser to load the spec fromString (generateFromString).

The parser therefore does not know the directory in which the spec is located in. It then fails to resolve relative paths.

What does work with the CLI is having an absolute path inside the root spec file. Taking the example from the linked issue and assuming the spec file is located in /home/user/projects/sample/index.yaml:

# index.yaml
asyncapi: 2.5.0
info:
  title: Example
  version: 0.1.0
channels:
  user/signedup:
    $ref: /home/user/projects/sample/subfolder/user-signedup.yaml

^ This would work. It is not very practical however when having the spec in a git repository that could be checked out with different paths as well.

I could not manage to get anything working with the fix inside the linked comment. It is possible that i misunderstood though. I tried file:subfolder/user-signedup.yaml and file://subfolder/user-signedup.yaml, both did not seem to work.

Laupetin avatar Jan 15 '24 14:01 Laupetin

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

sonarqubecloud[bot] avatar Jan 15 '24 14:01 sonarqubecloud[bot]

/update

peter-rr avatar May 17 '24 12:05 peter-rr

@peter-rr hey, thanks for checking it out. You are right, the exact case described in the issue seems to now generate correct output. When testing the new version on the documentation of our project i noticed however that this only applies if the index.yaml of the examplary setup is in your current workdirectory.

If the paths instead are docs/index.yaml and docs/subfolder/user-signedup.yaml then it will still fail to build (the resolved directory seems to be based on the workdirectory and not the directory containing the index.yaml). Trying using the generator instead of the cli as described in the issue does seem to work though.

Laupetin avatar May 17 '24 14:05 Laupetin

I rebased the branch on the current master branch. The following scenario does not work with the master branch but does with the changes of this PR (the index.yaml file is not in the current working directory):

# docs/index.yaml
asyncapi: 2.5.0
info:
  title: Example
  version: 0.1.0
channels:
  user/signedup:
    $ref: subfolder/user-signedup.yaml
# docs/subfolder/user-signedup.yaml
subscribe:
  message:
    description: An event describing that a user just signed up.
    payload:
      type: object
      additionalProperties: false
      properties:
        fullName:
          type: string
        email:
          type: string
          format: email
        age:
          type: integer
          minimum: 18

Laupetin avatar May 17 '24 15:05 Laupetin

/u

peter-rr avatar May 21 '24 10:05 peter-rr

/update

peter-rr avatar Jun 04 '24 09:06 peter-rr

/update

peter-rr avatar Jun 12 '24 08:06 peter-rr

/ptal

peter-rr avatar Jun 12 '24 10:06 peter-rr

@Amzani @derberg @magicmatatjahu @Souvikns @Shurtu-gal Please take a look at this PR. Thanks! :wave:

asyncapi-bot avatar Jun 12 '24 10:06 asyncapi-bot

/update

peter-rr avatar Jun 19 '24 10:06 peter-rr

/u

Shurtu-gal avatar Jun 24 '24 16:06 Shurtu-gal

Great catch @Laupetin 🚀, and thanks @peter-rr for looking into this as well. Passing path makes sense acc. to me.

Shurtu-gal avatar Jun 24 '24 16:06 Shurtu-gal

/u

peter-rr avatar Jun 25 '24 11:06 peter-rr

@Souvikns @Shurtu-gal @Amzani Seems like the issue related to SonarCloud workflow still persists: https://github.com/asyncapi/cli/issues/1470

FYI: I also opened this issue as a potential improvement to avoid this kind of scenarios: https://github.com/asyncapi/.github/issues/306

peter-rr avatar Jun 25 '24 11:06 peter-rr

@peter-rr for now we had to disable sonar cloud - to many problems comparing to the value it brings.

hope we can get alternative solution you reported done quickly

/rtm

derberg avatar Jul 16 '24 07:07 derberg

:tada: This PR is included in version 2.1.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

asyncapi-bot avatar Jul 16 '24 07:07 asyncapi-bot