Class SpringPageableScanUtils
- java.lang.Object
-
- org.openapitools.codegen.languages.SpringPageableScanUtils
-
public class SpringPageableScanUtils extends Object
Utility class for scanning OpenAPI specs for Spring Pageable-related features: sort enum validation, pageable defaults, and pageable constraints (max page/size).Can be used as a static utility or instantiated to hold the scan results (
sortValidationEnums,pageableDefaultsRegistry,pageableConstraintsRegistry) so that callers do not need to maintain those maps themselves. CallscanAll(OpenAPI, boolean)once inpreprocessOpenAPIto populate them, then access the fields directly.Used by both kotlin
KotlinSpringServerCodegenand javaSpringCodegento share scan and annotation-building logic. Only the mustache templates and their registration remain language-specific.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSpringPageableScanUtils.AnnotationSyntaxTarget language annotation syntax for pageable annotations.static classSpringPageableScanUtils.PageableConstraintsDataCarries max and min constraints for page number and page size from a pageable operation.static classSpringPageableScanUtils.PageableDefaultsDataCarries parsed default values for page, size, and sort fields from a pageable operation.static classSpringPageableScanUtils.SortFieldDefaultCarries a parsed sort field and its direction (always "ASC" or "DESC") from the spec default.
-
Field Summary
Fields Modifier and Type Field Description static List<String>DEFAULT_PAGEABLE_QUERY_PARAMSThe three Spring Data Web query-parameter names that together signal aorg.springframework.data.domain.Pageableoperation:page,size, andsort.static StringPAGEMap<String,SpringPageableScanUtils.PageableConstraintsData>pageableConstraintsRegistryMap from operationId to pageable constraints; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).Map<String,SpringPageableScanUtils.PageableDefaultsData>pageableDefaultsRegistryMap from operationId to pageable defaults; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).static StringSIZEstatic StringSORTMap<String,List<String>>sortValidationEnumsMap from operationId to allowed sort values; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).
-
Constructor Summary
Constructors Constructor Description SpringPageableScanUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleanapplyAutoXSpringPaginatedIfNeeded(io.swagger.v3.oas.models.Operation operation, boolean autoXSpringPaginated)Auto-detects Pageable pagination query parameters and, when detected, mutates the operation by settingx-spring-paginated: trueon its vendor extensions.voidapplyPageableAnnotations(CodegenOperation codegenOperation, boolean generatePageableConstraintValidation, boolean useBeanValidation, boolean generateSortValidation, SpringPageableScanUtils.AnnotationSyntax syntax)Instance variant ofapplyPageableAnnotations(CodegenOperation, boolean, boolean, Map, boolean, Map, Map, AnnotationSyntax)that uses the maps populated byscanAll(OpenAPI, boolean).static voidapplyPageableAnnotations(CodegenOperation codegenOperation, boolean generatePageableConstraintValidation, boolean useBeanValidation, Map<String,SpringPageableScanUtils.PageableConstraintsData> pageableConstraintsRegistry, boolean generateSortValidation, Map<String,List<String>> sortValidationEnums, Map<String,SpringPageableScanUtils.PageableDefaultsData> pageableDefaultsRegistry, SpringPageableScanUtils.AnnotationSyntax syntax)Builds and attaches the pageable-parameter annotations (@ValidPageable,@ValidSort,@PageableDefault,@SortDefault.SortDefaults) to the given codegen operation.static voidapplySpringDocPageableAnnotation(CodegenOperation codegenOperation, SpringPageableScanUtils.AnnotationSyntax syntax, boolean isSpringDoc)Applies SpringDoc-specific annotation handling for a pageable operation.static BooleangetXSpringPaginated(io.swagger.v3.oas.models.Operation operation)Returns the resolved `x-spring-paginated` flag.static voidremovePageableQueryParams(CodegenOperation codegenOperation)Removes the three Spring Data Web default pagination query parameters (page,size,sort) from the given codegen operation's parameter lists.voidscanAll(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)PopulatessortValidationEnums,pageableDefaultsRegistry, andpageableConstraintsRegistryby scanning the given OpenAPI document.static Map<String,SpringPageableScanUtils.PageableConstraintsData>scanPageableConstraints(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)Scans all pageable operations formaximum:andminimum:constraints onpageandsizeparameters.static Map<String,SpringPageableScanUtils.PageableDefaultsData>scanPageableDefaults(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)Scans all pageable operations for default values onpage,size, andsortparameters.static Map<String,List<String>>scanSortValidationEnums(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)Scans all pageable operations for asortparameter with enum values.static booleanwillBePageable(io.swagger.v3.oas.models.Operation operation, boolean autoXSpringPaginated)Returnstrueif the given operation will have a Pageable parameter injected — either because it hasx-spring-paginated: trueexplicitly, or becauseautoXSpringPaginatedis enabled and the operation has all three default pagination query parameters (page, size, sort).
-
-
-
Field Detail
-
PAGE
public static final String PAGE
- See Also:
- Constant Field Values
-
SIZE
public static final String SIZE
- See Also:
- Constant Field Values
-
SORT
public static final String SORT
- See Also:
- Constant Field Values
-
DEFAULT_PAGEABLE_QUERY_PARAMS
public static final List<String> DEFAULT_PAGEABLE_QUERY_PARAMS
The three Spring Data Web query-parameter names that together signal aorg.springframework.data.domain.Pageableoperation:page,size, andsort.Use this constant instead of repeating
Arrays.asList("page", "size", "sort")inline so that all callers stay in sync automatically.
-
sortValidationEnums
public Map<String,List<String>> sortValidationEnums
Map from operationId to allowed sort values; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).
-
pageableDefaultsRegistry
public Map<String,SpringPageableScanUtils.PageableDefaultsData> pageableDefaultsRegistry
Map from operationId to pageable defaults; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).
-
pageableConstraintsRegistry
public Map<String,SpringPageableScanUtils.PageableConstraintsData> pageableConstraintsRegistry
Map from operationId to pageable constraints; populated byscanAll(io.swagger.v3.oas.models.OpenAPI, boolean).
-
-
Method Detail
-
scanAll
public void scanAll(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)PopulatessortValidationEnums,pageableDefaultsRegistry, andpageableConstraintsRegistryby scanning the given OpenAPI document.Call this once from
preprocessOpenAPI(guarded by your library check), then read the public map fields infromOperation.- Parameters:
openAPI- the OpenAPI document to scanautoXSpringPaginated- whether auto-detection of pageable operations is enabled
-
applyPageableAnnotations
public void applyPageableAnnotations(CodegenOperation codegenOperation, boolean generatePageableConstraintValidation, boolean useBeanValidation, boolean generateSortValidation, SpringPageableScanUtils.AnnotationSyntax syntax)
Instance variant ofapplyPageableAnnotations(CodegenOperation, boolean, boolean, Map, boolean, Map, Map, AnnotationSyntax)that uses the maps populated byscanAll(OpenAPI, boolean).
-
willBePageable
public static boolean willBePageable(io.swagger.v3.oas.models.Operation operation, boolean autoXSpringPaginated)Returnstrueif the given operation will have a Pageable parameter injected — either because it hasx-spring-paginated: trueexplicitly, or becauseautoXSpringPaginatedis enabled and the operation has all three default pagination query parameters (page, size, sort).
-
getXSpringPaginated
public static Boolean getXSpringPaginated(io.swagger.v3.oas.models.Operation operation)
Returns the resolved `x-spring-paginated` flag.- Returns:
- `Boolean.TRUE`/`Boolean.FALSE` when explicitly set, otherwise `null`
-
applyAutoXSpringPaginatedIfNeeded
public static boolean applyAutoXSpringPaginatedIfNeeded(io.swagger.v3.oas.models.Operation operation, boolean autoXSpringPaginated)Auto-detects Pageable pagination query parameters and, when detected, mutates the operation by settingx-spring-paginated: trueon its vendor extensions.Detection is delegated to
willBePageable(Operation, boolean). If the operation is already explicitly flagged (x-spring-paginated: true/false) this method is a read-only pass-through — it returns the explicit value without mutating extensions. Only auto-detected operations (those whose flag wasnull) have the extension written.This method centralises the "detect + mutate" logic shared by both
SpringCodegenandKotlinSpringServerCodegeninside theirfromOperationoverrides. It must be called beforesuper.fromOperation()so that the base codegen can pick up the extension when populatingCodegenOperation.vendorExtensions.- Parameters:
operation- the raw OpenAPIOperationto inspect (and possibly mutate)autoXSpringPaginated- whether auto-detection is enabled for this generator- Returns:
trueif the operation is (or was just marked as) paginated
-
removePageableQueryParams
public static void removePageableQueryParams(CodegenOperation codegenOperation)
Removes the three Spring Data Web default pagination query parameters (page,size,sort) from the given codegen operation's parameter lists.When an operation is marked with
x-spring-paginated, Spring injects a singleorg.springframework.data.domain.Pageableparameter that internally handlespage,size, andsort. The individual query parameters must therefore be removed from the generated method signature.Callers are responsible for ensuring this is only invoked for server-side library configurations (spring-boot) where Pageable injection actually takes place.
- Parameters:
codegenOperation- the operation whose parameter lists should be pruned
-
applyPageableAnnotations
public static void applyPageableAnnotations(CodegenOperation codegenOperation, boolean generatePageableConstraintValidation, boolean useBeanValidation, Map<String,SpringPageableScanUtils.PageableConstraintsData> pageableConstraintsRegistry, boolean generateSortValidation, Map<String,List<String>> sortValidationEnums, Map<String,SpringPageableScanUtils.PageableDefaultsData> pageableDefaultsRegistry, SpringPageableScanUtils.AnnotationSyntax syntax)
Builds and attaches the pageable-parameter annotations (@ValidPageable,@ValidSort,@PageableDefault,@SortDefault.SortDefaults) to the given codegen operation.The annotations and their imports are only added when the relevant feature flags (
generatePageableConstraintValidation,generateSortValidation) are enabled and a matching entry exists in the corresponding registry for this operation. The result is stored under thex-pageable-extra-annotationvendor extension.Language-specific differences in annotation array syntax (Java
{...}vs Kotlin[...]) are handled by thesyntaxparameter. SpringDoc-specific import additions remain in each calling codegen class since they differ between Java (ParameterObject) and Kotlin (PageableAsQueryParam+x-operation-extra-annotationmutation).- Parameters:
codegenOperation- the operation to annotategeneratePageableConstraintValidation- whether to emit@ValidPageableuseBeanValidation- whether bean validation is activepageableConstraintsRegistry- per-operationId constraint datagenerateSortValidation- whether to emit@ValidSortsortValidationEnums- per-operationId allowed sort valuespageableDefaultsRegistry- per-operationId default page/size/sort datasyntax- target language annotation syntax
-
applySpringDocPageableAnnotation
public static void applySpringDocPageableAnnotation(CodegenOperation codegenOperation, SpringPageableScanUtils.AnnotationSyntax syntax, boolean isSpringDoc)
Applies SpringDoc-specific annotation handling for a pageable operation.When
isSpringDocistrue:- Java: adds the
ParameterObjectimport, which instructs SpringDoc to expose the individual pageable query parameters in the OpenAPI UI. - Kotlin: adds the
PageableAsQueryParamimport and prepends@PageableAsQueryParamto the operation'sx-operation-extra-annotationvendor extension so the Mustache template renders it on the generated controller method.
When
isSpringDocisfalsethis method is a no-op.- Parameters:
codegenOperation- the operation to annotatesyntax- target language annotation syntaxisSpringDoc- whether the active documentation provider is SpringDoc
- Java: adds the
-
scanSortValidationEnums
public static Map<String,List<String>> scanSortValidationEnums(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)
Scans all pageable operations for asortparameter with enum values.- Returns:
- map from operationId to list of allowed sort strings (e.g.
["id,asc", "id,desc"])
-
scanPageableDefaults
public static Map<String,SpringPageableScanUtils.PageableDefaultsData> scanPageableDefaults(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)
Scans all pageable operations for default values onpage,size, andsortparameters.- Returns:
- map from operationId to
SpringPageableScanUtils.PageableDefaultsData(only operations with at least one default are included)
-
scanPageableConstraints
public static Map<String,SpringPageableScanUtils.PageableConstraintsData> scanPageableConstraints(io.swagger.v3.oas.models.OpenAPI openAPI, boolean autoXSpringPaginated)
Scans all pageable operations formaximum:andminimum:constraints onpageandsizeparameters. Values are resolved throughallOfand$refschemas so that constraints defined on shared component schemas are honoured.- Returns:
- map from operationId to
SpringPageableScanUtils.PageableConstraintsData(only operations with at least one constraint are included)
-
-