Forum Discussion

DeepakPatankar's avatar
DeepakPatankar
Occasional Visitor
4 years ago

Swagger Documentation when the java class is deserialised from String input

In my request body, I have a field which takes a string as input but is converted into a java object while deserializing.

public class UserCredentials {
  @NotBlank String username;
  @NotNull ReferenceString passwordRef;
}

the passwordRef value is taken as a string and I used @JsonCreator to convert the string to ReferenceString object.

public class ReferenceString {
  private String identifier;
  private String type;

  @JsonCreator
  public ReferenceString(String secretRefConfigString) {
    // logic to break the string into two and set the
    // identifier and type value
    return;
  }

The serialization and deserialization are working fine. I am having an issue with the swagger documentation, the documentation looks like

    "UserCredentials" : {
        "type" : "object",
        "required" : [ "passwordRef" ],
        "properties" : {
          "username" : {
            "type" : "string"
          },
          "passwordRef" : {
            "$ref" : "#/definitions/ReferenceString"
          }
        }
    }
    "ReferenceString" : {
      "type" : "object",
      "properties" : {
        "identifier" : {
          "type" : "string"
        },
        "type" : {
          "type" : "string"
        }
      }
    }

Since the API will take a string as input, I want that in the docs the type of passwordRef should be shown as String and not ReferenceString, also I don't want the user to see the internal ReferenceString.

I read about @ApiModel and @ApiModelProperty. I can use the @ApiModelProperty on my fields and the documentation will show that field as a string, but in my code, a lot of fields will be of type ReferenceString and I don't want to use @ApiModelProperty on every field.

Can it be done easily some different way? Can I add some annotation/ code only on the ReferenceString to solve this issue?

No RepliesBe the first to reply