Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use java custom annotation in openapi spec 3

Just wondering if there is a way I can use my custom annotation in openapi specification.

Scenario-

User Object- I need to have notnull validation between username or email. either one of these should be provided in request

enter image description here

For this I can create a custom annotation in code something like @ValidateUserNameAndEmail, but not finding a way to apply this in openapi spec and include it like other annotation in generated code.

like image 894
Avhi Avatar asked Nov 03 '25 20:11

Avhi


1 Answers

I use the latest version (6.2.1) of openapi-generator-maven-plugin and following works for me.

User:
  type: object
  x-class-extra-annotation: "@org.example.ValidateUsernameAndEmail"
  ...

The plugin generates following class:

@Schema(name = "User ")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-11-24T14:51:09.277810200+01:00[Europe/Prague]")
@org.example.ValidateUsernameAndEmail
public class User {
    ...
}

You can also define a field-level annotation using x-field-extra-annotation if needed.

like image 77
Michal Avatar answered Nov 05 '25 09:11

Michal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!