springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.29k stars 499 forks source link

Hide the superclass from model resolver globally #2391

Closed zhahaoyu closed 1 year ago

zhahaoyu commented 1 year ago

Is your feature request related to a problem? Please describe. I am directly taking a 3rd party object as an input and as a response in my controller implementation. The 3rd party object has some convoluted super classes that cause the downstream documentation and client generator to fail.

Controller Implementation:

@PutMapping("/drafts/{draftId}")
fun updateDraft(
    @RequestParam("mailboxId") mailboxId: String,
    @PathVariable("draftId") draftId: String,
    @RequestBody draft: Draft
): Draft {
    return gmailService.updateDraft(mailboxId, draftId, draft)
}

Example 3rd party object:


package com.google.api.services.gmail.model;

/**
 * A draft email in the user's mailbox.
 *
 * <p> This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Gmail API. For a detailed explanation see:
 * <a href="https://developers.google.com/api-client-library/java/google-http-java-client/json">https://developers.google.com/api-client-library/java/google-http-java-client/json</a>
 * </p>
 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class Draft extends com.google.api.client.json.GenericJson {

  /**
   * The immutable ID of the draft.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String id;

  /**
   * The message content of the draft.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Message message;
}

Notice the reference to com.google.api.client.json.GenericJson

public class GenericJson extends GenericData implements Cloneable {

  /** JSON factory or {@code null} for none. */
  private JsonFactory jsonFactory;
}

this class introduces some weird dependencies and more weird attributes from its superclass GenericData and so forth.

Describe the solution you'd like

Since I do not create my own object, I cannot use @Schema annotation to hide the superclass. It would be nice to provide a global imperative way to exclude inheritance from certain classes or simply ignore/replace the superclass with a noop Object/Any class.

Ideally like this:

SpringDocUtils.getConfig().addSuperclassToIgnore(GenericJson::class.java)

or make the addJavaTypeToIgnore work with superclass

SpringDocUtils.getConfig().addJavaTypeToIgnore(GenericJson::class.java)

Describe alternatives you've considered Alternatively, I have to create mirror DTOs for every response.

bnasslahsen commented 1 year ago

@anar-orujov,

There are no current plans to support this feature. If you feel it useful for the community, feel free to propose directly a PR.