suman j
suman j

Reputation: 6980

exclude metaclass properties for groovy model classes in swagger

How do we exclude metaclass properties in model for "groovy" classes as Response? I have a Jax-Rs resource which returns a groovy object annotated with swagger @ApiModel. I see too many groovy specific attributes in swagger ui. How do I exclude it from serialization?


@EqualsAndHashCode
@Document(collection = 'MongoCollection')
@CompileStatic
@ToString
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
@ApiModel(value = "Represents a document from mongo collection")
class Foo {
..
..

}

It seems to be using Jackson for pogo-json serialization? How do annotate my groovy class to exclude metaclass properties from getting into json serialized string? I tried using JsonIgnoreProperties annotation but it didnt help.

@JsonIgnoreProperties(ignoreUnknown = true, value = ["MetaClass","MetaMethod"])

Upvotes: 3

Views: 2672

Answers (4)

Raytray
Raytray

Reputation: 17

https://springdoc.org/#groovy-support

Including this dependency with the springdoc-openapi-ui dependency will resolve the issue in the newer versions.

<dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-groovy</artifactId>
      <version>1.6.13</version>
   </dependency>

Upvotes: 1

btpka3
btpka3

Reputation: 3830

If using springfox, see springfox issues 752, found a way to resolve this :

docket.ignoredParameterTypes(groovy.lang.MetaClass.class)

A code example is:

@Configuration
public class SpringFoxConfig {
     @Bean
     public Docket api() {
         return new Docket(DocumentationType.SWAGGER_2)
             .ignoredParameterTypes(groovy.lang.MetaClass.class)
             .select()
             .apis(RequestHandlerSelectors.any())
             .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
             .paths(PathSelectors.any())
             .build();
        }
    }

Upvotes: 6

JDiPierro
JDiPierro

Reputation: 802

Upgrading Swagger to a version >= 1.3.5 should resolve this.

Upvotes: -1

suman j
suman j

Reputation: 6980

This could be a bug in swagger as per https://github.com/wordnik/swagger-core/issues/519. I switched from groovy to java classes for model objects to proceed as of now. Will work on creating a test for this issue when I get time.

Upvotes: 0

Related Questions