Swagger core ignores @JsonValue and @XmlValue?

631 views
Skip to first unread message

Rupert Smith

unread,
Mar 1, 2016, 7:23:40 AM3/1/16
to Swagger
I read here that swagger core will read and process JAXB annotations:

I have this in my model code:

@JsonValue
@XmlValue
public RefDataItem getAsJson() {
   ...
}

I am using this to return a reference data item, simply an object with just an 'id' in it, or an 'id' and a 'name'. Internally the reference data item is a complex class with fields that I want hidden from the outside view of my API.

The @XmlValue JAXB annotation is ignored by swagger though, and all of the internal fields are exposed in the json schema.

Is there a way I can say for this class in my model, substitute a different one (RefDataItem)?

Perhaps I need to use ApiModelProperty to explicitly hide all the internal fields. But I would also need a way to substitute in the 'id' and 'name' fields... 

Seems to me that swagger could maybe do with something extra in the ApiModel annotation:

ApiModel(value = "RefDataItem", schemaClass = RefDataItem.class)

perhaps?

Rupert

Ron Ratovsky

unread,
Mar 1, 2016, 2:26:05 PM3/1/16
to swagger-sw...@googlegroups.com
JAXB annotations is not supported out of the box, but you can add it. Please see https://github.com/swagger-api/swagger-core/issues/960#issuecomment-100484695.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rupert Smith

unread,
Mar 1, 2016, 2:44:42 PM3/1/16
to Swagger
On Tuesday, March 1, 2016 at 7:26:05 PM UTC, Ron wrote:
JAXB annotations is not supported out of the box, but you can add it. Please see https://github.com/swagger-api/swagger-core/issues/960#issuecomment-100484695.

Thanks, I'll try that. 

Rupert Smith

unread,
Mar 4, 2016, 12:05:28 PM3/4/16
to Swagger
It didn't work. 

It stopped all the internal fields being exposed, but just produced an empty {} in the schema, instead of the schema model for RefDataItem.

Rupert Smith

unread,
Mar 8, 2016, 11:06:50 AM3/8/16
to Swagger
I was able to solve my problem using a ModelConverter instead. I used a marker interface on the classes that I am interested in, and by substituting RefDataItem.class for them instead, I get the json-schema for that in the swagger instead.

Works nicely. I have now learnt that if the swagger annotations don't cut it, a custom ModelConverter is the way to do. I will need a few more of these converters to handle other tricky aspects of my model.

Here is the 'resolve' method for the one for the class substitution:

    public Model resolve(Type type, ModelConverterContext modelConverterContext,
        Iterator<ModelConverter> iterator) {

        if (type instanceof SimpleType) {
            SimpleType simpleType = (SimpleType) type;

            Class<?> rawClass = simpleType.getRawClass();

            if (EnumType.class.isAssignableFrom(rawClass)) {
                SimpleType refDataType = SimpleType.construct(RefDataItem.class);

                return super.resolve(refDataType, modelConverterContext, iterator);
            }
        }

        return super.resolve(type, modelConverterContext, iterator);
    }
Reply all
Reply to author
Forward
0 new messages