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

Add ability to customize classes schemas in properties. #840

Merged
merged 1 commit into from
Nov 16, 2022
Merged

Add ability to customize classes schemas in properties. #840

merged 1 commit into from
Nov 16, 2022

Conversation

altro3
Copy link
Collaborator

@altro3 altro3 commented Nov 12, 2022

Now you can set custom classes to create different open api schemas for selected classes:

micronaut:
  openapi:
    schema:
      org.somepackage.MyComplexType: java.lang.String
      org.somepackage.MyComplexType2: java.lang.Integer

or by system properties:

-Dmicronaut.opemapi.schema.org.somepackage.MyComplexType=java.lang.String -Dmicronaut.opemapi.schema.org.somepackage.MyComplexType2=java.lang.Integer

or by openapi.properties

micronaut.opemapi.schema.org.somepackage.MyComplexType=java.lang.String
micronaut.opemapi.schema.org.somepackage.MyComplexType2=java.lang.Integer

Also It can be used for replace classes schema with generics, for example, if you use jaxb generated classes and have custom serializer for JAXBElement class (see tests). And you can set cutom schemas for different type args. For example if you have this classes structure:

package test.mypackage;

class MyDto {

    public JAXBElement<? extends XmlElement> xmlElement;
    public JAXBElement<? extends XmlElement2> xmlElement2;
    public JAXBElement<? extends XmlElement3> xmlElement3;
}

package test.mypackage;

class XmlElement {
    public String propStr;
}

package test.mypackage;

class XmlElement2 {
    public String propStr2;
}

package test.mypackage;

class XmlElement3 {
    public String propStr3;
}

You can customize classes structure for openapi schema:

package io.micronaut.openapi;

// if you want to use generic from fields with type JAXBElement<T>
class MyJaxbElement<T> {
    public String type;
    public T value;
}

package io.micronaut.openapi;

class MyJaxbElement2 {
    public String type;
    public List<String> values;
}

package io.micronaut.openapi;

class MyJaxbElement3 {
    public String type;
    public String value;
}

And set openapi properties to map classes to custom openapi schema classes:

micronaut:
  openapi:
    schema:
      io.micronaut.openapi.JAXBElement: io.micronaut.openapi.MyJaxbElement
      io.micronaut.openapi.JAXBElement<test.mypackage.XmlElement2>: io.micronaut.openapi.MyJaxbElement2
      io.micronaut.openapi.JAXBElement<test.mypackage.XmlElement3>: io.micronaut.openapi.MyJaxbElement3

Fixed #654

@altro3
Copy link
Collaborator Author

altro3 commented Nov 12, 2022

@graemerocher Hi! could you merge and release this very important feature?

@graemerocher
Copy link
Contributor

these should be specified as annotation processor inputs otherwise the compiler will not pick up the changes if you change any of the values.

@altro3
Copy link
Collaborator Author

altro3 commented Nov 14, 2022

@graemerocher Sorry, I didn't understand what you mean. My idea is that you specify in the configuration which classes in the API structure you want to replace with other classes. The compiler is not required for this action, all class structures are loaded from the context. In addition, I tested all these changes locally on my project. I still don't understand what could be the problem.

@graemerocher
Copy link
Contributor

in general this module does have problems with incremental compile since everything seems to be configured via openapi.properties so maybe this is a broader concern. The issue though is that if you change openapi.properties and the sources haven't changed, running the compilation (without first doing a clean) will not recompile the classes in order to pick up the changes, unless you supply the correct inputs to the compiler through the use of annotation processor arguments.

@altro3
Copy link
Collaborator Author

altro3 commented Nov 14, 2022

Hmm... Could you show an example how to check if you are right or not. And maybe you can suggest how to fix this problem.

In general, this feature turned out to be really very important. In my project, I ran into the problem of generating JAXBElement<? extends SomeGeneratedClass>. I added a custom serializer to jackson so that the field data is correctly represented in json, but I realized that I can’t generate the correct swagger file in any way, because. The code is generated and I can't change it. With the help of this feature, it will be possible to fix this problem

@altro3
Copy link
Collaborator Author

altro3 commented Nov 15, 2022

@graemerocher Hi! So what do you end up suggesting? This feature is very important and without it at the moment it is impossible to work with the generated files. If there are suggestions on how to improve my solution - no problem, I will do it.

@graemerocher
Copy link
Contributor

Looks like the feature is missing documentation in general, so please add documentation and in the documentation state that changing the configuration requires a full recompile. Thanks.

@altro3
Copy link
Collaborator Author

altro3 commented Nov 15, 2022

@graemerocher done

@altro3
Copy link
Collaborator Author

altro3 commented Nov 16, 2022

@graemerocher Hi! Sorry to rush, but this feature is really very important. In addition, I found a small bug and it can also be included in the release. Could you merge everything and release it?

@graemerocher graemerocher added the type: enhancement New feature or request label Nov 16, 2022
@graemerocher
Copy link
Contributor

since this is a new feature could you bump the minor version and update the target branch to 3.8.x

Thanks

@altro3
Copy link
Collaborator Author

altro3 commented Nov 16, 2022

@graemerocher done!

@graemerocher graemerocher merged commit 8738541 into micronaut-projects:master Nov 16, 2022
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.

Convert types (for example ObjectId)
2 participants