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

Groovy metaClass not ignored when model is built for deserialization #752

Closed
aleksz opened this issue May 23, 2015 · 16 comments
Closed

Groovy metaClass not ignored when model is built for deserialization #752

aleksz opened this issue May 23, 2015 · 16 comments

Comments

@aleksz
Copy link

aleksz commented May 23, 2015

    @RequestMapping(value = "/myresource", method = RequestMethod.GET)
    MyResource get() {
    }

    @RequestMapping(value = "/myresource", method = RequestMethod.POST)
    MyResource save(@Valid @RequestBody MyResource myResource) {
    }

When I have only "get", in my controller - MyResource does not have metaClass. As soon as I add "save", metaClass appears in both calls.

swagger-annotations:1.5.3-M1
swagger-model:1.5.3-M1
springfox-swagger2:2.0.0
jackson:2.4.5

Tracked to this code in BeanModelPropertyProvider:

  private BeanDescription beanDescription(ResolvedType type, ModelContext context) {
    if (context.isReturnType()) {
      SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
      return serializationConfig.introspect(TypeFactory.defaultInstance()
          .constructType(type.getErasedType()));
    } else {
      DeserializationConfig serializationConfig = objectMapper.getDeserializationConfig();
      return serializationConfig.introspect(TypeFactory.defaultInstance()
          .constructType(type.getErasedType()));
    }
  }

In both cases introspect returns metaClass description

@dilipkrish
Copy link
Member

You should probably apply a mix in to all your groovy models with the @JsonIgnoredProperties(["metaClass"] annotation.

@aleksz
Copy link
Author

aleksz commented May 25, 2015

This does not help

@dilipkrish
Copy link
Member

Weird. Do you have an example repo? Another thing u could try is to add the groovy Metaclass To the ignorable types in your Docket If nothing works feel free to reopen this issue.

@aleksz
Copy link
Author

aleksz commented May 25, 2015

Do you mean Docket.ignoredParameterTypes. It says:

   * Adds ignored controller method parameter types so that the framework does not generate swagger model or parameter
   * information for these specific types.
   * e.g. HttpServletRequest/HttpServletResponse which are already included in the pre-configured ignored types.

Should there be something else?

@dilipkrish
Copy link
Member

Yes, u can add the groovy Metaclass to the ignorables using that method.

@aleksz
Copy link
Author

aleksz commented May 26, 2015

I think this method does what JavaDoc says, ignores parameter types, not
properties with that type :) OperationParameterReader.readParameters seems
to be the place. Sorry if i miss something.

On Mon, May 25, 2015 at 11:18 PM, Dilip Krishnan notifications@github.com
wrote:

Yes, u can add the groovy Metaclass to the ignorables using that method.


Reply to this email directly or view it on GitHub
#752 (comment)
.

@aleksz
Copy link
Author

aleksz commented May 26, 2015

Tried one more thing:

    @JsonIgnoreType
    class IgnoreTypeMixIn {
        //
    }

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
        builder.mixIn(MetaClass, IgnoreTypeMixIn)
        return builder
    }

still same effect. Also changing MetaClass to String affects serialized properties, but not Swagger documentation.

@dilipkrish
Copy link
Member

I've added tests that demonstrate there is no issue in the library as-is. I suspect you've customized your ObjectMapper which in turn is causing the groovy meta class to show

@aleksz
Copy link
Author

aleksz commented Jun 2, 2015

it seems to me that this test does not test problematic case. I'm having issue with de-serialization, so you need to use that Groovy model as request body, not response.

@aleksz
Copy link
Author

aleksz commented Jun 2, 2015

I think I know why global mix in has no effect. Spring Boot has multiple MappingJackson2HttpMessageConverter converters. One is default, registered by AllEncompassingFormHttpMessageConverter. Another one is added by JacksonAutoConfiguration. As a result, FieldModelPropertyProvider.onApplicationEvent is called multiple times and last wins. Unfortunately, this is not the one from JacksonAutoConfiguration. Maybe ObjectMapperConfigurer should handle only primary one?

@dilipkrish dilipkrish reopened this Jun 4, 2015
@dilipkrish dilipkrish added this to the 2.0.2 milestone Jun 4, 2015
@dilipkrish
Copy link
Member

Thanks for pursuing the issue. This should now be fixed

dilipkrish added a commit that referenced this issue Jun 4, 2015
dilipkrish added a commit that referenced this issue Jun 4, 2015
…erage build issue

Uncovered the fact that the field model properties provider was working
incorrectly. This fixes that problem also removed some ignored tests.

related to #752
@aleksz
Copy link
Author

aleksz commented Jun 15, 2015

Thanks for fixing. Any plans to release it soon?

@dilipkrish
Copy link
Member

Hopefully end of this week

@aleksz
Copy link
Author

aleksz commented Jun 17, 2015

Verified that ignoredParameterTypes(MetaClass) does the job. However, there is still an issue with picking wrong ObjectMapper, which I believe is the root cause. Any mix ins or other configuration applied to correct ObjectMapper will be ignored.

@dilipkrish
Copy link
Member

@aleksz have you used the guidance in the docs to configure the ObjectMapper? If you do it that way or create ObjectMapper using the Jackson2ObjectMapperBuilder you shouldn't run into that issue.

@fitzf
Copy link

fitzf commented Jul 4, 2017

exclude metaClass properties for groovy model classes in swagger:
docket.ignoredParameterTypes(groovy.lang.MetaClass.class)

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

3 participants