Skip to content

Provide a public API to generate Schemas from Classes #3336

Description

@BernhardPosselt

My use case is programmatically registering additional endpoints that are handled in filters. Similar to how Form Login is handled, you might need custom login endpoints that take different parameters and return different responses from filters.

Right now, the only way to do that (correct me if I'm wrong) is to programmatically build up the schema, e.g.:

Schema<?> schema = new ObjectSchema()
  .addProperty(usernamePasswordAuthenticationFilter.getUsernameParameter(), new StringSchema())
  .addProperty(usernamePasswordAuthenticationFilter.getPasswordParameter(), new StringSchema())
  .required("username", "password");

What I'd like to have is a way to build up the component schema from types reusing built-in functionality. What I've currently landed upon is the following:

// this API does not properly handle nullability
SpringDocAnnotationsUtils.resolveSchemaFromType(
    SomeClass::class.java,
    it.components,
    null,
).required(getRequiredFields(SomeClass::class).toList())

/**
 * Note that this method does not respect Jackson's @JsonProperty or Kotlin Serialization's @SerialName
 */
fun <T : Any> getRequiredFields(clazz: KClass<T>): Set<String> = clazz.memberProperties
    .filterNot { it.returnType.isMarkedNullable }
    .map { it.name }
    .toSet()

But of course that piece of code does not work recursively and neither resolves Kotlin serialization annotations nor Jackson ones. From what I've seen so far in the code base, the only way to make this work is to start from controller methods. There is no way to just build up a schema from a type alone.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions