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

Better handling of dot in inline model name #3498

Merged
merged 3 commits into from
Jul 31, 2019
Merged

Better handling of dot in inline model name #3498

merged 3 commits into from
Jul 31, 2019

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Jul 30, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

Better handling of non-alphanumeric characters in inline model naming.

Fixes #3260

Tested locally with the spec provided by the user and the issue no longer exists.

cc @OpenAPITools/generator-core-team

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
private String resolveModelName(String title, String key) {
if (title == null) {
return uniqueName(key);
return uniqueName(key).replaceAll("[^A-Za-z0-9]", "_");
Copy link
Member

Choose a reason for hiding this comment

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

are $ or _ not allowed to be in the model name?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think $ would mean something else in some scripting languages

Copy link
Member

Choose a reason for hiding this comment

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

@wing328 how about _?

Copy link
Member Author

Choose a reason for hiding this comment

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

Very good question. The spec doesn't restrict the use of symbols (e..g $, _, .) in model naming.

This is auto-generated model name based on the parent model name and the property name. The issue occurs when the parent model name contains ..

Since the model name is auto-generated if title attribute is not present, this fix will simply get rid of non-alphanumeric characters to make our life easier.

If the user wants complete control of the model naming, they can still use title (35e49b9)

Copy link
Contributor

@richardwhiuk richardwhiuk Jul 30, 2019

Choose a reason for hiding this comment

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

Why does it matter if this name has a dot in it? Should we instead change uniqueName so it doesn't generate a name with a dot in it? It feels like the valid characters in the model name is a problem for the generator, not the core code?

In the case in question, it feels like the natural name would be models::io::kimmin::InlineFailure, exposing the namespaced path in the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why does it matter if this name has a dot in it?

It's causing a bug later that the model (created on the fly) cannot be looked up. Again this is auto-generated "schema" name, which will be used to determine the model name using toModelName.

This change has no impact on the schema name in the spec provided by the user.

@jimschubert
Copy link
Member

jimschubert commented Jul 30, 2019

I think this might be getting done too close to spec reading, making the change far too restrictive.

The title in JSON Schema is not necessarily intended to be used as a model name (http://json-schema.org/latest/json-schema-validation.html#rfc.section.10.1). This would be equivalent to using summary to generate an operationId in a Path object.

It seems this would mean that users can't define model names such as user_name directly in the schema without adding the optional (and redundant) title: user_name. I know this will break several of my existing specs, and will require that I add support for title to custom documentation builders (in Finagle). I imagine it's a similar breaking change for many others.

Also, while not a common use case, it means we disallow any Unicode characters in model names.

I'm wondering if we could add the fix in the CodegenConfig instance instead?

@wing328
Copy link
Member Author

wing328 commented Jul 30, 2019

It seems this would mean that users can't define model names such as user_name directly in the schema without adding the optional (and redundant) title: user_name

They can. This change will still auto-generate the model name as "user_name" if the parent model name is "user" and the property name is "name".

To be clear, this is not the ultimate (inline) model name users will see in the auto-generated code. For "user_name", users will instead find the model "UserName" in auto-generated Java client/server-side code.

The title in JSON Schema is not necessarily intended to be used as a model name (http://json-schema.org/latest/json-schema-validation.html#rfc.section.10.1).

I agree title may not be the best attribute to name the model and this PR does not change the usage of title in the current implementation.

@jimschubert
Copy link
Member

@wing328 I see. We may want a comment about that behavior in this code. It seems by looking at the code that we're transforming spec document content at a point before generators have a say in the matter, which I think should be avoided.

@wing328
Copy link
Member Author

wing328 commented Jul 31, 2019

@jimschubert added more comments via 38d772b

@wing328 wing328 merged commit d46cabc into master Jul 31, 2019
@wing328 wing328 deleted the inline_dot branch July 31, 2019 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Inline model resolving failure when the model name contains dot .
5 participants