swagger-api / swagger-api/swagger-codegen-generators
Regex string is not escaped when a property refers to a schema
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
Regex string is not escaped when a property refers to a schema
https://github.com/swagger-api/swagger-codegen-generators/blob/master/src/main/java/io/swagger/codegen/v3/generators/util/OpenAPIUtil.java#L25-L27
public static void addPropertiesFromRef(OpenAPI openAPI, Schema refSchema, CodegenProperty codegenProperty) {
final Map<String, Schema> allSchemas = openAPI.getComponents().getSchemas();
if (allSchemas == null || allSchemas.isEmpty()) {
return;
}
final Schema schema = allSchemas.get(getSimpleRef(refSchema.get$ref()));
if (schema == null) {
return;
}
if (StringUtils.isBlank(codegenProperty.pattern)) {
// pattern here is not escaped
codegenProperty.pattern = schema.getPattern();
}
codegenProperty.minLength = schema.getMinLength();
codegenProperty.maxLength = schema.getMaxLength();
if (codegenProperty.pattern != null || codegenProperty.minLength != null || codegenProperty.maxLength != null) {
codegenProperty.getVendorExtensions().put(HAS_VALIDATION_EXT_NAME, Boolean.TRUE);
}
}
Potential fix
public static void addPropertiesFromRef(CodegenConfig codegenConfig, OpenAPI openAPI, Schema refSchema, CodegenProperty codegenProperty) {
final Map<String, Schema> allSchemas = openAPI.getComponents().getSchemas();
if (allSchemas == null || allSchemas.isEmpty()) {
return;
}
final Schema schema = allSchemas.get(getSimpleRef(refSchema.get$ref()));
if (schema == null) {
return;
}
if (StringUtils.isBlank(codegenProperty.pattern)) {
// use toRegularExpression method in CodegenConfig to escape
codegenProperty.pattern = codegenConfig.toRegularExpression(schema.getPattern());
}
codegenProperty.minLength = schema.getMinLength();
codegenProperty.maxLength = schema.getMaxLength();
if (codegenProperty.pattern != null || codegenProperty.minLength != null || codegenProperty.maxLength != null) {
codegenProperty.getVendorExtensions().put(HAS_VALIDATION_EXT_NAME, Boolean.TRUE);
}
}
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
Start in src/main/java/io/swagger/codegen/v3/generators/util/OpenAPIUtil.java at addPropertiesFromRef and inspect its callers before changing the method inputs. Use CodegenConfig.toRegularExpression for a schema's pattern while preserving the existing validation metadata behavior; verify the affected generator behavior and any relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100