Skip to content

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

Description

@justinplus

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions