0

i am using open api generator for request and response objects generation. I want to hide/ignore the json field from yaml. So i have tried by using this config in yaml-> x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonIgnore" or import io.swagger.v3.oas.annotations.Hidden. Below is the generated code

    @JsonProperty("id")
    private String id;
    @JsonProperty("name")
    @JsonIgnore
    private String name;

and for Hidden

    @JsonProperty("id")
    private String id;
    @JsonProperty("name")
    @Hidden
    private String name;

But i can see name in response. Please suggest how to ignore or hide the name field from response.

veera
  • 317
  • 2
  • 3
  • 14
  • https://stackoverflow.com/questions/53263086/how-to-hide-a-request-field-in-swagger-api – cool Jun 23 '23 at 11:52
  • @cool how to set APIModelProperty(hidden = true) for that field in yaml? – veera Jun 23 '23 at 12:12
  • it looks like I misunderstood your question. When you say you want to hide that field from response do you mean when the value is not provided don't send it? If so you should set this annotation on the field @JsonInclude(JsonInclude.Include.NON_NULL) – cool Jun 23 '23 at 12:19
  • @cool tried like this x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonInclude(JsonInclude.Include.NON_NULL)" but getting compilation error saying Include does not exist. Library also there. – veera Jun 23 '23 at 12:55
  • 1
    try this x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)" – cool Jun 23 '23 at 14:30
  • @cool It worked.. thanks.. – veera Jun 24 '23 at 07:06

1 Answers1

1

Create a Views class and create two views – Public and Private:

    public class Views {
        public static class Public {}
    
        public static class Private {}
    }

after that annoted name field with @JsonView like below.

    @JsonView(Views.Private.class)
    private String name;

I think it's work fine for you.

Sahil Patel
  • 534
  • 1
  • 6
  • I'm generating java class from yaml. Can you suggest how to set value for that field in yaml – veera Jun 23 '23 at 12:18
  • can you please share your yaml file here?? – Sahil Patel Jun 23 '23 at 12:35
  • ```ResponseDTO: type: object properties: emp: type: object properties: id: type : string name: type: string x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonIgnore" ``` – veera Jun 23 '23 at 12:50