swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.37k stars 2.17k forks source link

Make ModelConverterContext swappable #4718

Open nikita2206 opened 1 month ago

nikita2206 commented 1 month ago

Currently, the ModelConverterContext is instantiated statically from within the ModelConverters class. The context happens to contain some logic that seems to be hard (or impossible) to modify in any other way, other than by extending the ModelConverterContextImpl and overriding the resolve() method.

Problem

Namely, we would like to add clever handling of Optional<...> properties of objects, such that the OAS component that contains such property would specify nullable = true for it. However, because the ModelConverterContextImpl has the following line, we lose the information about Optional wrapper very early on, which makes it impossible for us to use the existing ModelConverter extension points to implement this:

AnnotatedType aType = ReferenceTypeUtils.unwrapReference(type);

Solution

If you don't see any immediate issues with this, I'd like to create a PR adding a ModelConverterContextFactory factory which will be settable on a ModelConverters singleton object, like the following:

ModelConverters.getInstance()
  .setContextFactory(converters -> new CustomContext(converters));