swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

Regex string is not escaped when a property refers to a schema

Open justinplus opened this issue 1 year ago • 0 comments

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);
        }
    }

justinplus avatar Dec 12 '24 13:12 justinplus