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: parse Avro name field to custom x-parser-schema-id #69

Merged
merged 12 commits into from
Sep 2, 2021

Conversation

damaru-inc
Copy link
Contributor

Description
The 'name' field is required in Avro schemas, but this parser does not make that available in the converted JSON schema. With this change, the Avro name is copied to the JSON schema's title field.

Related issue(s)
Resolves #68

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.

Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@damaru-inc damaru-inc changed the title feat: The Avro name field is now copied to the Json schema title field. feat: the Avro name field is now copied to the Json schema title field. Aug 25, 2021
@derberg derberg changed the title feat: the Avro name field is now copied to the Json schema title field. feat: parse Avro name field to custom x-parser-schema-id Aug 27, 2021
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.

I left few comments and code change suggestions

to-json-schema.js Outdated Show resolved Hide resolved
tests/schemas/Person-1.9.0-namespace.avsc Outdated Show resolved Hide resolved
tests/schemas/Person-1.9.0-namespace.avsc Outdated Show resolved Hide resolved
tests/schemas/Person-1.9.0-namespace.avsc Outdated Show resolved Hide resolved
tests/schemas/Person-1.8.2.avsc Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@damaru-inc
Copy link
Contributor Author

Okay, I've addressed all the comments.

@damaru-inc damaru-inc requested a review from derberg August 27, 2021 15:26
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.

left some new comments, also there are still some code changes suggestions that were not yet applied, to fix json files with "

to-json-schema.js Show resolved Hide resolved
if (avroDefinition.doc) jsonSchema.description = avroDefinition.doc;
if (avroDefinition.default !== undefined) jsonSchema.default = avroDefinition.default;

const fullyQualifiedName = getFullyQualifiedName(avroDefinition);
if (isTopLevel && fullyQualifiedName !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (isTopLevel && fullyQualifiedName !== undefined) {
if (isTopLevel && !fullyQualifiedName) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change that if you like, but I was just trying to be consistent with the style. Just above on line 26 we have:
if (avroDefinition.default !== undefined) jsonSchema.default = avroDefinition.default;

Copy link
Member

Choose a reason for hiding this comment

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

oh, I think now I remember why it was avroDefinition.default !== undefined, because probably avroDefinition.default could be null which should be fine 🤔 sorry but can you change it back and add a proper code comment?

README.md Outdated

If, at the top level of the Avro schema, the 'name' attribute is defined, it will be copied to the corresponding JSON schema's 'x-parser-schema-id' attribute. If the Avro schema also has the 'namespace' attribute defined, then that schema's fully qualified name will be put into that attribute. The fully qualified name is defined by the namespace, followed by a dot, followed by the name.

If there are two schemas that resolve to the same fully qualified name, only the second schema will be returned by the parser, the first will be discarded.
Copy link
Member

Choose a reason for hiding this comment

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

I have to say this one doesn't let me sleep 😄
what part of the code is responsible for it? why 2nd is returned, then what if 3 schemas resolve to the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume that parser-js keeps a map where the key is x-parser-schema-id. So if there are several schemas that share the same x-parser-schema-id, then only the last one parsed will end up in that map. But if their document has several schemas with the same fully qualified name, I'd hope that they're identical, they repeat them because we force them to put Avro schemas into the message/payload section instead of components/schemas.

Maybe I should just not mention that at all? Or maybe that's an argument for using the title field after all, and adding x-namespace or something?

Copy link
Member

Choose a reason for hiding this comment

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

ah, now it makes sense, you are referring to 👇🏼

  /**
   * @returns {Map<string, Schema>}
   */
  allSchemas() {
    const schemas = new Map();
    const allSchemasCallback = (schema) => {
      if (schema.uid()) {
        schemas.set(schema.uid(), schema);
      }
    };
    traverseAsyncApiDocument(this, allSchemasCallback);
    return schemas;
  }

I think the sentence is ok, but just maybe write
If there are two schemas that resolve to the same fully qualified name, only the last one will be returned by the parser. Make sure names of your schemas are unique ?

@damaru-inc damaru-inc requested a review from derberg August 30, 2021 14:13
derberg
derberg previously approved these changes Sep 2, 2021
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.

sorry @MichaelDavisSolace for the late review, one small readme stuff and I think we can merge and observe how the earth stands still 😄

@damaru-inc
Copy link
Contributor Author

sorry @MichaelDavisSolace for the late review, one small readme stuff and I think we can merge and observe how the earth stands still 😄

Thanks, I assume you mean that bit about the parser discarding schemas that have the same qualified name - should I just remove that?

@derberg
Copy link
Member

derberg commented Sep 2, 2021

@damaru-inc oh, you here? what time is it there man!

Thanks, I assume you mean that bit about the parser discarding schemas that have the same qualified name - should I just remove that?

no just change to something similar I suggested there

@damaru-inc
Copy link
Contributor Author

@damaru-inc oh, you here? what time is it there man!

Thanks, I assume you mean that bit about the parser discarding schemas that have the same qualified name - should I just remove that?

no just change to something similar I suggested there

I'll make that change right now. It's 5:00 AM and I can't sleep.

derberg
derberg previously approved these changes Sep 2, 2021
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 2, 2021

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

@derberg derberg merged commit 14090ef into asyncapi:master Sep 2, 2021
@damaru-inc damaru-inc deleted the issues/68-copy-name-to-title branch September 2, 2021 14:13
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

The Avro Schema 'name' field should get carried over to the JSON Schema 'title' field.
4 participants