Description
springdoc-openapi
supports Pageable, so it make sense to support Sort as well.
Seeing #1186 and #1618, and now needing it myself, there is quite a bit of demand for it.
@bnasslahsen I would like to contribute a PR for it, but would first like to consult you on what is the best approach.
From reading the existing implementation, since Sort
is represented as a single String[]
parameter, the easiest way would be to implement a ModelConverter
that returns a new ArraySchema().items(new StringSchema())
, e.g.
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (SORT_TO_REPLACE.equals(cls.getCanonicalName())) {
if (!type.isSchemaProperty())
return new ArraySchema().items(new StringSchema());
else
// also, what's the purpose of this?
type.name(cls.getSimpleName() + StringUtils.capitalize(type.getParent().getType()));
}
}
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
}
However, I think it would be nice if the resolved parameter would have the same description
as Pageable#sort
.
Would it make sense to create a model class for only 1 parameter? e.g.
public class Sort {
@Parameter(description = "Sorting criteria in the format: property,(asc|desc). "
+ "Default sort order is ascending. " + "Multiple sort criteria are supported."
, name = "sort"
, array = @ArraySchema(schema = @Schema(type = "string")))
private List<String> sort;
public List<String> getSort() {
return sort;
}
public void setSort(List<String> sort) {
if (sort == null) {
this.sort.clear();
}
else {
this.sort = sort;
}
}
}
Or would it be better to create an anonymous class of ArraySchema
and override ArraySchema#getDescription()
? e.g.
return new ArraySchema() {
@Override
public String getDescription() {
return "Sorting criteria in the format: property,(asc|desc). Default sort order is ascending. Multiple sort criteria are supported.";
}
@Override
public Schema<?> getItems() {
return new StringSchema();
}
};
or create a subclass of ArraySchema
?
public class SortSchema extends ArraySchema {
private Schema<?> items = new StringSchema();
private String description = "Sorting criteria in the format: property,(asc|desc). Default sort order is ascending. Multiple sort criteria are supported.";
@Override
public Schema<?> getItems() {
return items;
}
@Override
public void setItems(Schema<?> items) {
this.items = items;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
}
but I don't see any such existing patterns, so I'm sure if its the right way to do it.