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

Allow component names to contain underscores #268

Merged
merged 8 commits into from
Sep 9, 2024

Conversation

augustuswm
Copy link
Contributor

When handling schema components that were generated from types containing generics, you often get components that have names like TypeX_for_TypeY. Currently the name matching regex does not match against the underscore and instead only captures up through TypeX.

This change allows for the _ character in type names.

I'm unsure where the appropriate place to write a test for this is. I can put one together if pointed in the right direction.

@@ -21,7 +21,7 @@ export const getSortedSchemas = (spec: OpenAPIV3.Document) => {
Object.keys(spec.components?.schemas || {}).map((name) => [
name,
JSON.stringify(spec.components!.schemas![name])
.match(/#\/components\/schemas\/[A-Za-z0-9]+/g)
.match(/#\/components\/schemas\/[A-Za-z0-9_]+/g)
Copy link
Collaborator

@david-crespo david-crespo Sep 9, 2024

Choose a reason for hiding this comment

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

Maybe this could just be like .+? or \w+. or even .startsWith('#/components/schemas/')? No need to be this specific to begin with

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me. I can see if the Open API spec has any guidance on allowed characters.

Copy link
Contributor Author

@augustuswm augustuswm Sep 9, 2024

Choose a reason for hiding this comment

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

I think this answers it:

All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$.

@david-crespo
Copy link
Collaborator

Lol it resorted everything

@augustuswm
Copy link
Contributor Author

Well know I want to know why...

@david-crespo
Copy link
Collaborator

I don't think this is working right. Every entry in the list has undefined for the second thing (the result of the JSON.stringify().match().map(), with or without this change.

export const getSortedSchemas = (spec: OpenAPIV3.Document) => {
  return topologicalSort(
    Object.keys(spec.components?.schemas || {}).map((name) => [
      name,
      JSON.stringify(spec.components!.schemas![name])
        .match(/#\/components\/schemas\/[a-zA-Z0-9.\-_]+$/g)
        ?.map((s) => s.replace("#/components/schemas/", "")),
    ])
  );
};

@david-crespo
Copy link
Collaborator

Thanks!

@david-crespo david-crespo merged commit 52e9ccf into main Sep 9, 2024
3 checks passed
@david-crespo david-crespo deleted the allow-underscore_in_name branch September 9, 2024 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants