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

feat: map schema references from baseUrl to folder #529

Merged
merged 38 commits into from
May 14, 2021

Conversation

muenchhausen
Copy link
Contributor

@muenchhausen muenchhausen commented Mar 7, 2021

Description

introduce a new cli parameter

--map-base-url <url>:<baseFolder>: maps all schema references with this base url to folder

This can be tested by running

./cli.js test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/:./test/docs/

Related issue(s)
Resolves #512

Open

  1. Run the test from above as unit or integration test. How do you want me to implement that? Should I test against cli.js or generator.js?
  2. There is a lint Warning in generator.js 92:3: How do you want me to refactor this?

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it! I'd only suggest removing all these console.log before it's merged :)

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good! Please refer to my comments and also, please write some integration tests using examples from your PR in test/docs :)

CONTRIBUTING.md Outdated Show resolved Hide resolved
cli.js Outdated Show resolved Hide resolved
lib/generator.js Outdated Show resolved Hide resolved
@magicmatatjahu
Copy link
Member

@muenchhausen Answering to your questions:

Run the test from above as unit or integration test. How do you want me to implement that? Should I test against cli.js or generator.js?

You can write test only for Generator class. Please pass mapBaseUrlToFolder property to Generator's constructor, parse given schema with refs and check by some asserts that references are properly parsed :)

There is a lint Warning in generator.js 92:3: How do you want me to refactor this?

If you move new logic from constructor to separate function, it should be fixed.

@derberg derberg changed the title feat: map schema references from baseUrl to folder - improved feat: map schema references from baseUrl to folder Mar 10, 2021
@derberg
Copy link
Member

derberg commented Mar 10, 2021

Please add information about new CLI param to the readme

@derberg
Copy link
Member

derberg commented Mar 17, 2021

@muenchhausen please have a look at the failing tests

@muenchhausen
Copy link
Contributor Author

muenchhausen commented Mar 21, 2021

I did not manage to create an integration test that checks the renedered result for test/docs/apiwithref.json. I got some problems with some mocked libraries - even if I remove some jest.mock. I have the feeling, that integration testing is a sepearate issue.

Here is the test I tried:

const path = require('path');
const Generator = require('../lib/generator');

//jest.mock('../lib/filtersRegistry');
//jest.mock('../lib/hooksRegistry');

describe('Generator', () => {
  describe('#generateFromFile', () => {

    it('test', async () => {
      const gen = new Generator('@asyncapi/html-template', '/Users/z0024p1y/prototypes/generator/build', {
        mapBaseUrlToFolder: 'https://schema.example.com/crm/:./test/docs/',
        forceWrite: true
      });
      await gen.generateFromFile(path.resolve('./test/docs/apiwithref.json'));
    });
  });
});

@magicmatatjahu
Copy link
Member

@muenchhausen Hi! Thanks for your work! I see that you have very old revision in tests in your branch:

Please update branch and write tests like here https://github.com/asyncapi/generator/blob/master/test/integration.test.js :)

fmvilas
fmvilas previously approved these changes May 6, 2021
lib/generator.js Outdated Show resolved Hide resolved
magicmatatjahu
magicmatatjahu previously approved these changes May 7, 2021
lib/generator.js Outdated Show resolved Hide resolved
Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
@muenchhausen muenchhausen dismissed stale reviews from magicmatatjahu and fmvilas via 4c06419 May 7, 2021 17:47
@muenchhausen
Copy link
Contributor Author

muenchhausen commented May 10, 2021

I implemented all suggestions and tested this branch successfully in a company CI. I think I am done again :)

// it splits on last occurrence of : into the groups all, url and folder
const re = /(.*):(.*)/g;
let mapping = [];
if ((mapping = re.exec(v))===null || mapping.length!==3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a comment that this statement will not work with such situation:

ag test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/

because https://schema.example.com/crm/ has : so const re = /(.*):(.*)/g; returns 3 elements and therefor it contunes to work and fails when trying to resolve refs

Something went wrong:
Error: Error downloading https://schema.example.com/crm/shared.json 
getaddrinfo ENOTFOUND schema.example.com
    at dereference (/Users/wookiee/sources/generator/node_modules/@asyncapi/parser/lib/parser.js:155:11)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - good point! I fixed it. Of course it would be better to put cli.js under test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we will have all that is needed in the new CLI that will be not only for the generator but also other tools -> https://github.com/asyncapi/cli

this is why we do not invest much in CLI tests here

@derberg
Copy link
Member

derberg commented May 10, 2021

@muenchhausen I played with this on local and just found this https://github.com/asyncapi/generator/pull/529/files#r629460080. Other than that - great stuff 🎉

@fmvilas @magicmatatjahu have a final look folks

Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🚀

@sonarcloud
Copy link

sonarcloud bot commented May 14, 2021

Kudos, SonarCloud 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

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@derberg derberg merged commit c369b85 into asyncapi:master May 14, 2021
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@derberg
Copy link
Member

derberg commented May 14, 2021

@all-contributors please add @muenchhausen for code

@allcontributors
Copy link
Contributor

@derberg

I've put up a pull request to add @muenchhausen! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

map schema base url to local folder
5 participants