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

New feature: schema decorators #885

Merged
merged 4 commits into from
Dec 14, 2022

Conversation

altro3
Copy link
Collaborator

@altro3 altro3 commented Dec 13, 2022

If you have some classes with same names in different packages you can set postfix like this:

micronaut:
  openapi:
    schema-postfix:
      org.api.v1_0_0: 1_0_0
      org.api.v2_0_0: 2_0_0

or by system properties:

-Dmicronaut.openapi.schema-postfix.org.api.v1_0_0=1_0_0 -Dmicronaut.openapi.schema-postfix.org.api.v2_0_0=2_0_0

or by openapi.properties

micronaut.openapi.schema-postfix.org.api.v1_0_0=1_0_0
micronaut.openapi.schema-postfix.org.api.v2_0_0=2_0_0

…ecorators

# Conflicts:
#	openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiApplicationVisitor.java
@altro3
Copy link
Collaborator Author

altro3 commented Dec 14, 2022

@graemerocher Hi! Could you merge this PR and release a new 4.9.0 release?

I ran into a problem on my project. I have generated API models according to the xsd scheme. There can be up to 10 such schemes, each for its own version of the API. The problem is that micronaut-openapi does not take into account when generating schemas that the classes are in different packages, but the classes are named the same. Because of this, the final swagger file is generated incorrectly.

This is one of the methods for solving this problem, suitable for me. In the future, I will add the ability to specify a behavior strategy for classes with the same name, i.e. so that, for example, an index is automatically added to the end of the model name, as is now done with duplicate operationId

return javaName.replace("$", ".");
String packageName = fullClassName.substring(0, fullClassName.lastIndexOf('.'));
OpenApiApplicationVisitor.SchemaDecorator schemaDecorator = OpenApiApplicationVisitor.getSchemaDecoration(packageName, context);
javaName = javaName.replace("$", ".");
Copy link
Contributor

Choose a reason for hiding this comment

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

you should use getCanonicalName() on the element instead of this hack

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean? If you are talking about substring by dollar symbol, then this is not my code, it was written that way. And I didn't understand how you suggest to use canoncialName ? getCanoncialName returns the full name of the class along with the package, but I don't need it, I need either the class name or the package name.

In any case, I changed the logic for calculating the package name.

@@ -218,6 +218,25 @@ public class OpenApiApplicationVisitor extends AbstractOpenApiVisitor implements
*/
private static final String MICRONAUT_OPENAPI_SCHEMA = "micronaut.openapi.schema";
private static final String MICRONAUT_CUSTOM_SCHEMAS = "micronaut.internal.custom.schemas";
/**
* Properties prefix to set schema name prefixe or postfix by package.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo prefixe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@graemerocher graemerocher merged commit eb95cfc into micronaut-projects:4.8.x Dec 14, 2022
@graemerocher graemerocher added the type: enhancement New feature or request label Dec 14, 2022
@altro3
Copy link
Collaborator Author

altro3 commented Dec 15, 2022

@graemerocher Thanks! :-) Could you release all fixes ?

@graemerocher
Copy link
Contributor

Done, thanks for the contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants