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

Anonymous schemas should have meaningful ids #176

Closed
jonaslagoni opened this issue Oct 1, 2020 · 16 comments
Closed

Anonymous schemas should have meaningful ids #176

jonaslagoni opened this issue Oct 1, 2020 · 16 comments
Labels
enhancement New feature or request stale

Comments

@jonaslagoni
Copy link
Member

Reason/Context

We at Eurisco are not a big fan of the anonymous schema ids. They don't give any context to what anonymous schema or properties they are apart of which makes it hard to track down where it belongs. Instead we replace any anonymous schema ids to be the path to the object, see example solution for more details.

Description

The function giving schemas and messages anonymous ids need to be changed to another algorithm.

This will be breaking change in the regard anonymous schema ids are no longer present and people have to update their implementations unless they manually defined all schemas.

Example solution

My colleague developed a custom hook for changing the anonymous schema ids recursively by saving the path to that schema.

module.exports = {
    'generate:before': async generator => {
        preprocess(generator, generator.asyncapi)
    }
};

preprocess = (generator, obj, lastKey = "", lastSchemaID = "") => {
    // Fix anonymous x-parser-schema-id
    const schemaId = obj["x-parser-schema-id"]
    if (schemaId !== undefined) {
        if (schemaId.startsWith("<anonymous-schema-")) {
            const newSchemaId = lowerFirst(lastSchemaID + upperFirst(lastKey))
            obj["x-parser-schema-id"] = newSchemaId
            lastSchemaID = newSchemaId
        } else {
            lastSchemaID = schemaId
        }
    }
    const keys = Object.keys(obj)
    for (let i = 0; i < keys.length; i++) { 
        let key = keys[i]
        const value = obj[key];
        if (typeof value === 'object' && value != undefined) {
            let newKey = key
            if (key == "payload" && obj["name"] !== undefined && lastKey === "message") {
                lastSchemaID = obj["name"]
            } else if (key == "items" && obj["type"] === "array") {
                newKey = ""
            }
            if(!!obj['x-parser-circular-props']){
                continue;
            }
            preprocess(generator, value, newKey, lastSchemaID);
        }
    }
    return obj
}
upperFirst = (str) => {
    return str.charAt(0).toUpperCase() + str.slice(1);
};
lowerFirst = (str) => {
    return str.charAt(0).toLowerCase() + str.slice(1);
};

together with the hook and the following AsyncAPI document

asyncapi: '2.0.0'
info:
  title: Questionaire
  version: '1.0.0'
defaultContentType: application/json
channels:
  questionaire/get:
    description: "Get the newest version of the questionaires"
    publish:
      message:
        name: questionaire
        payload:
            type: object 
            x-parser-schema-id: questionaire
            properties:
                steps: 
                    type: object
                    additionalProperties: 
                        type: string

It would normally give the inner property steps of questionaire the id <anonymous-schema-x> but with the hook it will get questionaireSteps.

@jonaslagoni jonaslagoni added the enhancement New feature or request label Oct 1, 2020
@fmvilas
Copy link
Member

fmvilas commented Oct 1, 2020

Love it! I wonder how would this work when the same schema is referenced from multiple places 🤔 Are you really going to generate an id for each of them? If so, that could be counterproductive for model code generation, because you'll get the same model many times with different names. Anyway, it will always be better than having model file names like AnonymousSchema3 😅

@jonaslagoni jonaslagoni changed the title Anonymous schemas should have meaningfull ids Anonymous schemas should have meaningful ids Oct 1, 2020
@github-actions
Copy link

github-actions bot commented Dec 1, 2020

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 30 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions
Copy link

github-actions bot commented Apr 3, 2021

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions github-actions bot added the stale label Apr 3, 2021
@derberg derberg removed the stale label Apr 6, 2021
@github-actions
Copy link

github-actions bot commented Jun 6, 2021

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions
Copy link

github-actions bot commented Aug 7, 2021

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions github-actions bot added the stale label Aug 7, 2021
@derberg derberg removed the stale label Aug 9, 2021
@github-actions
Copy link

github-actions bot commented Dec 8, 2021

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Dec 8, 2021
@derberg
Copy link
Member

derberg commented Dec 13, 2021

@jonaslagoni where are we with this one ?

@jonaslagoni
Copy link
Member Author

Have not taken the time to solve it (again)... Gonna keep a focus on it in asyncapi/parser-api#34 as this change is breaking.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Apr 14, 2022
@derberg
Copy link
Member

derberg commented Apr 20, 2022

this is still planned for 3.0 parser ?

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Jun 18, 2023
@trevordixon
Copy link

trevordixon commented Aug 16, 2023

So far this works for our schema, in case it's helpful. It didn't work when setting key to '' for an array as above. It was clashing with some other type and I was getting buggy output. Renaming the key to 'item' makes sense to me.

export function renameAnonymousSchemas(obj: AsyncAPIDocumentInterface, lastKey = '', lastSchemaID = '', seenObjs = []) {
  if (obj['x-parser-schema-id']?.startsWith('<anonymous-schema-')) {
    obj['x-parser-schema-id'] = lowerFirst(lastSchemaID + upperFirst(lastKey));
  }
  lastSchemaID = obj['x-parser-schema-id'] || lastSchemaID;
  for (let [key, value] of Object.entries(obj)) {
    if (typeof value !== 'object') continue;
    if (seenObjs.includes(value)) continue;
    if (key === 'payload' && obj['name'] && lastKey === 'message') {
      lastSchemaID = obj['name'];
    } else if (key == 'items' && obj['type'] === 'array') {
      key = 'item';
    }
    renameAnonymousSchemas(value, key, lastSchemaID, [...seenObjs, value]);
  }
}

If you can suggest a better way, perhaps using better APIs, I'd appreciate it.

@github-actions github-actions bot removed the stale label Aug 18, 2023
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Dec 16, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants