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

Type name collision fix #601

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

SRetip
Copy link
Contributor

@SRetip SRetip commented May 30, 2024

Problem:
If we try to generate schema from this schema:

{
    "$schema": "http:https://json-schema.org/draft-04/schema#",
    "definitions": {
        "Foo": {
            "properties": {
                "Bar": {
                    "oneOf": [
                        {
                            "$ref": "#/definitions/FooBar"
                        },
                        {
                            "$ref": "#/definitions/Baz"
                        }
                    ]
                }
            },
            "type": "object"
        },
        "FooBar": {
            "type": "string"
        },
        "Baz": {
            "type": "string"
        }
    }
}

we get:

pub struct Baz(pub String);

pub struct Foo {
    pub bar: Option<FooBar>,
}

pub struct FooBar(pub String);

pub enum FooBar {
    FooBar(FooBar),
    Baz(Baz),
}

where struct FooBar - structure from definition:

  "FooBar": {
            "type": "string"
        },

and enum FooBar - enum which come from property definition:

 "Bar": {
                    "oneOf": [
                        {
                            "$ref": "#/definitions/FooBar"
                        },
                        {
                            "$ref": "#/definitions/Baz"
                        }
                    ]
                }

and as a result, we have a name conflict.

With my solution result will be:

pub struct Baz(pub String);

pub struct Foo {
    pub bar: Option<FooBarAlias>,
}

pub struct FooBar(pub String);

pub enum FooBarAlias {
    FooBar(FooBar),
    Baz(Baz),
}

I'd be happy to have a feedback :)

Copy link
Collaborator

@ahl ahl left a comment

Choose a reason for hiding this comment

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

I appreciate that this solves for the case you have in mind, but the solution seems fairly narrow. For example, it applies to add_ref_types, but not to add_type. I'm concerned that successively suffixing "Alias" isn't going to produce types that are particularly usable.

Comments describing your approach would be helpful. Its placement was a little confusing. For example, we're doing this during the type finalization pass... but I'm not sure this has anything to do with type finalization.

@SRetip
Copy link
Contributor Author

SRetip commented Jun 27, 2024

If I understand you correctly, I have corrected what was required:

  • put this logic in a separate method for TypeEntry;
  • added the call of this function to the TypeSpace::add_type_with_name(...) method;
  • added comments:
// I think we should do it this way, because in this place we're iterating over all 
// created types.

As for adding "Alias", I honestly can't think of a way to make it more user friendly. But otherwise the code will simply not compile without this.

P.S. The "Alias" post-fix is generated only when there is a name conflict.

@SRetip SRetip requested a review from ahl June 27, 2024 09:20
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.

None yet

2 participants