Closed
Description
Using Springfox 2.8.0
According to the docs, substituting List<MyClass>
with List<String>
should be done like this:
Q. How does one create rules to substitute list and array items?
If the following types…ToSubstitute[] array; List<ToSubstitute> list;
Need to look like this over the wire…
Substituted[] array; List<Substituted> list;
This is how the rules need to be configured
rules.add(newRule(resolver.arrayType(ToSubstitute), resolver.arrayType(Substituted))) rules.add(newRule(resolver.resolve(List, ToSubstitute), resolver.resolve(List, Substituted)))
However this configuration prevents the JSON file from being generated:
.alternateTypeRules(AlternateTypeRules.newRule(
typeResolver.resolve(List.class, MyClass.class),
typeResolver.resolve(List.class, String.class)
))
This is the output stacktrace:
java.lang.NullPointerException: the Function passed to Optional.transform() must not return null.
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787) ~[guava-20.0.jar:na]
at com.google.common.base.Present.transform(Present.java:75) ~[guava-20.0.jar:na]
at springfox.documentation.schema.ModelRef.getItemType(ModelRef.java:76) ~[springfox-core-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ParameterMapper.fromModelRef(ParameterMapper.java:61) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ParameterMapper.bodyParameter(ParameterMapper.java:46) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ParameterMapper.mapParameter(ParameterMapper.java:41) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.parameterListToParameterList(ServiceModelToSwagger2MapperImpl.java:223) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapOperation(ServiceModelToSwagger2MapperImpl.java:153) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper.mapOperations(ServiceModelToSwagger2Mapper.java:173) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper.mapApiListings(ServiceModelToSwagger2Mapper.java:155) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapDocumentation(ServiceModelToSwagger2MapperImpl.java:56) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(Swagger2Controller.java:92) ~[springfox-swagger2-2.8.0.jar:2.8.0]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
How the problem appeared:
- In Springfox 2.6.0 we had a configuration to replace
MyClass
withString
(using directModelSubstitute). It worked correctly for the simple type, but did not replaceList<MyClass>
withList<String>
(which we didn't notice at first) - After migrating to Springfox 2.8.0, errors started to appear, because it seems the directModelSubstitute made the definition of
MyClass
disappear from the generated JSON:
Resolver error at definitions.MyDto.properties.myfield.$ref
Could not resolve reference because of: Could not resolve pointer: /definitions/MyClass does not exist in document
- To fix this, we introduced the mapping to substitute list of items and discovered that it prevented the whole JSON file from being generated.
It works correctly if a non-primitive type is used instead of String, so a workaround is to replace String.class with WildcardType.class, but the resulting documentation is not correct.