OpenAPITools / OpenAPITools/openapi-generator
[BUG] rust-server: client feature missing lazy_static and regex deps when models use pattern validation
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When an OpenAPI spec contains models with pattern validation (regex constraints on string properties), the generated models.rs emits lazy_static::lazy_static! blocks and regex::Regex usages unconditionally — they are not gated behind any feature flag.
However, in the generated Cargo.toml, the client feature does not include "lazy_static" or "regex" in its dependency list (unless hasCallbacks is true). The server feature correctly includes them.
This means a consumer that enables only features = ["client"] (without "server") gets compile errors when any model in the spec uses pattern:
error[E0433]: failed to resolve: use of undeclared crate or module lazy_static
error[E0433]: failed to resolve: use of undeclared crate or module regex
Root Cause
In Cargo.mustache (line ~47):
client = [
...
{{#hasCallbacks}}
"serde_ignored", "percent-encoding", {{^apiUsesByteArray}}"lazy_static", "regex",{{/apiUsesByteArray}}
{{/hasCallbacks}}
"hyper", "percent-encoding", "hyper-util/http1", "hyper-util/http2", "url"
]
The lazy_static and regex features are only added to client when hasCallbacks is true (for server-path routing in callback handling).
But in models.mustache (line ~471):
{{#vars}}
{{#hasValidation}}
{{#pattern}}
{{^isByteArray}}
lazy_static::lazy_static! {
static ref RE_...: regex::Regex = regex::Regex::new(r"{{ pattern }}").unwrap();
}
{{/isByteArray}}
{{/pattern}}
{{/hasValidation}}
{{/vars}}
These model-level regex statics are emitted for any feature combination — they're part of the shared models.rs that both client and server use
openapi-generator version
Present across all 7.x releases
OpenAPI declaration file content or url
openapi: "3.0.3"
info:
title: Test API
version: "1.0.0"
paths:
/phone:
get:
summary: Get phone
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/PhoneNumber"
components:
schemas:
PhoneNumber:
type: string
pattern: '^+[1-9]\d{1,14}$'
Generation Details
Generate with rust-server:
java -jar openapi-generator-cli.jar generate
-i test-spec.yaml
-g rust-server
-o output/
Then in the consuming crate, depend on the generated crate with client-only:
my-api = { path = "../output", default-features = false, features = ["client"] }
Build fails:
- run: cargo build
- output: error[E0433]: failed to resolve: use of undeclared crate or module
lazy_static - output: error[E0433]: failed to resolve: use of undeclared crate or module
regex
No special additional properties or config needed — any spec with a pattern constraint on a model triggers it when using only the client feature (without server).
Steps to reproduce
- Create an OpenAPI spec with a model that has a pattern constraint:
components:
schemas:
PhoneNumber:
type: string
pattern: '^+[1-9]\d{1,14}$' - Generate a Rust crate using rust-server generator
- In a consuming crate, depend on the generated crate with only features = ["client"]
- Build → compile errors on lazy_static and regex
Related issues/PRs
Suggest a fix
Either:
A) Add "lazy_static", "regex" to the client feature unconditionally (matching server):
client = [
...
"hyper", "percent-encoding", "hyper-util/http1", "hyper-util/http2", "url",
{{^apiUsesByteArray}}"lazy_static", "regex"{{/apiUsesByteArray}}
]
B) Gate the model lazy_static! blocks behind #[cfg(any(feature = "server", feature = "client", feature = "validate"))] — though this is more complex and may break validation.
Option A is simpler and matches the existing server feature pattern.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read Cargo.mustache and models.mustache, comparing the client and server feature dependency lists. Generate the supplied OpenAPI example with rust-server, then build a consumer with only the client feature. Done when that client-only crate compiles despite a model pattern constraint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100