Add the possibility to customize the SwaggerV20Library
Issue #306
resolved
Hey!
Guys, it'd be nice to have to customize at least format attributes in the SwaggerV20Library . For example, I want to support my
@Schema(format = "phone")
and I don't want to use the "pattern" for that purposes. As of now I can't add a PhoneFormatAttribute to handle that format.
It can be smth like:
public class SwaggerV20Library {
public static final String OAI_V2_METASCHEMA_URI = "https://openapis.org/specification/versions/2.0#";
private static final Map<String, AbstractFormatAttribute> CUSTOM_FORMAT_ATTRIBUTES = new ConcurrentHashMap<>();
public static Map<String, AbstractFormatAttribute> getCustomFormatAttributes() {
return ImmutableMap.copyOf(CUSTOM_FORMAT_ATTRIBUTES);
}
public static void upsertFormatAttributes(String format, AbstractFormatAttribute attributeHandler) {
CUSTOM_FORMAT_ATTRIBUTES.put(format, attributeHandler);
}
static Library get() {
// The discriminator validator holds state that may persist in the event of a runtime exception etc.
// Re-create the library to ensure this state doesn't persist between validations.
LibraryBuilder libraryBuilder = DraftV4Library.get().thaw()
.addFormatAttribute("int32", Int32Attribute.getInstance())
.addFormatAttribute("int64", Int64Attribute.getInstance())
.addFormatAttribute("float", FloatAttribute.getInstance())
.addFormatAttribute("double", DoubleAttribute.getInstance())
.addFormatAttribute("date", DateAttribute.getInstance())
.addFormatAttribute("byte", Base64Attribute.getInstance())
.addFormatAttribute("phone", PhoneFormatAttribute.getInstance())
.addKeyword(Nullable.getInstance())
.addKeyword(Discriminator.getInstance());
for (var entry : CUSTOM_FORMAT_ATTRIBUTES.entrySet()) {
libraryBuilder.addFormatAttribute(entry.getKey(), entry.getValue());
}
return libraryBuilder
.freeze();
}
....... some other lines
}
P.S. can I commit into your repo?
Comments (2)
-
-
- changed status to resolved
Available in v2.32.0
- Log in to comment
This is an interesting idea!
In general I have no problem with it, but I would suggest that instead of modifying the existing
SwaggerV20Library
it would be cleaner to allow a new library to be specified during construction of the validator, with some factory method to extend the existing library with your custom attributes.Please feel free to fork the repo and raise a PR (see CONTRIBUTING.md for details).
Cheers