Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ApiModel has no way to hide properties on super classes and compile time enhanced classes #730

Closed
bthule opened this issue Oct 28, 2014 · 25 comments
Milestone

Comments

@bthule
Copy link

bthule commented Oct 28, 2014

@ApiModelProperty allows hiding properties that in my model class, but there is no way to hide properties brought in by super classes or compile time enhanced classes.

For instance, Ebean enhances its model classes, and when they are used as a response Class, swagger-ui shows them with an extra property "_ebean_intercept (EntityBeanIntercept, optional)", and all of its properties and sub-properties.

@webron
Copy link
Contributor

webron commented Oct 28, 2014

Can you provide with a sample models (super+inherited)?

@bthule
Copy link
Author

bthule commented Oct 28, 2014

The compile time enhanced is my primary issue-- I included the super class property issue since a solution would likely solve both:

Here is sample model for super+inherited classes:

// A super class provided by third-party
public class Animal {
    String uselessAnnoyingProperty;
}
// A sub class whose api-model should not have uselessAnnoyingProperty
public class Dog extends Animal {
    int numberOfLegs;
}

swagger-ui would show:

{
    "numberOfLegs": 0,
    "uselessAnnoyingProperty": "",
}

The solution I would suggest is:

@ApiModel(hideProperties = {"uselessAnnoyingProperty", ...})

@webron
Copy link
Contributor

webron commented Oct 28, 2014

Right, but in this specific example, it sounds like you'd want to hide the "uselessAnnoyingProperty" anyways, so why not add @ApiModelProperty to it?

@bthule
Copy link
Author

bthule commented Oct 28, 2014

Because it is a super class provided by a third party. For me, right now I just need to hide properties, but I can see valid cases where others would want to be able to have all the options available in @ApiModelProperty. So a better solution might be:

@ApiModel(properties = {@ApModelProperty(name="uselessAnnoyingProperty", hide=true), ...})

@webron
Copy link
Contributor

webron commented Oct 28, 2014

It's probably not a relevant solution as with the next version of swagger-core, Jackson will be responsible entirely for the de/serialization process. However, we'll look into it.

@bthule
Copy link
Author

bthule commented Oct 28, 2014

Will swagger-core also work with Moxy? Jersey 2 now prefers Moxy, which uses different annotations than Jackson. Moxy uses jaxb annotations such as @XmlRootElement.

Anyway, I'd like to be sure you read that the issue I am directly facing right now is with compile time enhancing-- (I included the super/inherited class issue since I believe any solution would likely solve both).

I am fine with waiting for next version of swagger-core. I found a (yucky) workaround by adding this to my api model class:

    @ApiModelProperty(hidden=true)
    public int _ebean_intercept;  // not used, except to trick swagger-core

Once ebean compile enhances, it will add a _ebean_intercept of type EntityBeanIntercept. Swagger still uses the annotations on the _ebean_intercept of type int. This workaround seems to be working okay.

@webron
Copy link
Contributor

webron commented Oct 28, 2014

Jackson processes JAXB's annotations as well, so it should work fine. There's probably not going to be direct support for Moxy just like there's not going to be direct support for Gson.

As for your issue, there's another workaround which won't be less ugly, so I'm not sure it's worth mentioning.

@fehguy fehguy added the Feature label Dec 20, 2014
@fehguy
Copy link
Contributor

fehguy commented Feb 1, 2015

will investiagte for m2

@fehguy fehguy added this to the v1.5.0-M2 milestone Feb 1, 2015
@webron webron added the P2 label Mar 10, 2015
@fehguy
Copy link
Contributor

fehguy commented Mar 11, 2015

This has been fixed as of 1.5.2-M1

@fehguy fehguy closed this as completed Mar 11, 2015
@aciganj
Copy link

aciganj commented Oct 21, 2016

I probably missed something, but how is it fixed? Is it now possible to write something like bthule suggested:

@ApiModel(hideProperties = {"uselessAnnoyingProperty", ...})?

I can't seem to find that feature.

@fehguy
Copy link
Contributor

fehguy commented Oct 21, 2016

@aciganj use @ApiModelProperty(hidden=true)

@aciganj
Copy link

aciganj commented Oct 22, 2016

You mean I should add a property of the same name as the one I want to hide and then use that annotation? I guess that would work for private properties, but what about others? Is there a way to do it without adding properties or getters to subclass?

@bthule
Copy link
Author

bthule commented Oct 23, 2016

@aciganj Did you try @apimodel(properties = {@ApModelProperty(name="uselessAnnoyingProperty", hidden=true), ...})

@fehguy
Copy link
Contributor

fehguy commented Oct 24, 2016

I see @aciganj, I misunderstood your question. Please try what @bthule suggested and if it doesn't work, open a new ticket.

@aciganj
Copy link

aciganj commented Oct 24, 2016

@bthule , @fehguy
@apimodel doesn't have the properties option, and other options don't seem useful for this purpose. I'll create a new issue

@duckladydinh
Copy link

Please reopen the issue. This is a very important issue for the documentation because otherwise, the json data and the swagger model will not match. Please reopen the issue.

@r3215407
Copy link

r3215407 commented Nov 9, 2018

it is a very important issus for me, Please reopen the issue.

@duckladydinh
Copy link

it is a very important issus for me, Please reopen the issue.

Hi, I think the same and I tried implementing some workaround for it in this pull request #2996, but unfortunately, Swagger team does not allow open contribution, so it will never be official. But if you do not intend to use Swagger forever, maybe it will help for now.

@wenyi189
Copy link

It is 2019 now. And this is still not supported. I'm running into the exact problem where I have multiple sub types of APIException. And this APIException extends from RuntimeException from java. For example, if I have a subtype called UnexpectedException and it extends APIException, everything from parent class got picked up including the fields in Throwable.class. Very annoying and still couldn't find a way to get it working.

@thomasmchen
Copy link

I want to second @wenyi189 , having support for this case would be exceptionally beneficial for anyone using third party libraries or who cannot access parent classes to annotate fields they would like to ignore. This definitely seems within the scope of Swagger's problem space.

@MGZero
Copy link

MGZero commented May 17, 2019

Same thing here, I have a common resource class with some endpoints that should not come out in swagger for a particular project, but it should for others. I can't currently do that without overriding the endpoint methods in the one project. I shouldn't have to do that though just for documentation purposes.

@PankajAmbekar
Copy link

I think the 'hide-superclass-fields' feature would be a good value add for documentation

@max91
Copy link

max91 commented Dec 3, 2020

The year 2020 was ending ... I'm amazed at how much time has passed since this feature was designated as very important. And just as amazed that she still does not exist. We urgently need to reopen this task.

For example, there is such a thing Spring HATEOAS. In which resources are sometimes superfluous are all sorts of different things. Which it would be nice not to show in the documentation for the request body. So, the swagger does not provide an opportunity to customize it. This is the answer to a six-year old question as an example. And in general, to be honest, how old is the swagger and how many problems it has. It clearly doesn't look like a java library =/

@jsiddharth
Copy link

This is a very important feature. From 2 aspects
Consider basic code below

class Base {
    private int uselessparameter for docs ;
}
class Inherited extends Base {
    private int usefulparameter from docs;
}

Case 1)
Now Base may be in my control, so I should be able to use @APIIgnore and it should work.

Case 2)
Base is a library and I want to hide it when I extend Base in Inherited. Even then I should be allowed that.

@blackwood821
Copy link

I tried annotating my super class bean property with @Schema(hidden = true) which didn't work, but adding it to the getter method of that property got it to no longer show up in Swagger UI.

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

No branches or pull requests