Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Microsoft.OpenApi/Validations/Rules/OpenApiParameterRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ public static class OpenApiParameterRules
context.Exit();
});

/// <summary>
/// Validate that a path parameter should always appear in the path
/// </summary>
public static ValidationRule<OpenApiParameter> PathParameterShouldBeInThePath =>
new ValidationRule<OpenApiParameter>(
(context, parameter) =>
{
if (parameter.In == ParameterLocation.Path && !context.PathString.Contains("{" + parameter.Name + "}"))
{
context.Enter("in");
context.CreateError(
nameof(PathParameterShouldBeInThePath),
$"Declared path parameter \"{parameter.Name}\" needs to be defined as a path parameter at either the path or operation level");
context.Exit();
}
});
// add more rule.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ namespace Microsoft.OpenApi.Validations.Rules
{
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiParameter> ParameterMismatchedDataType { get; }
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiParameter> ParameterRequiredFields { get; }
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiParameter> PathParameterShouldBeInThePath { get; }
public static Microsoft.OpenApi.Validations.ValidationRule<Microsoft.OpenApi.Models.OpenApiParameter> RequiredMustBeTrueWhenInIsPath { get; }
}
[Microsoft.OpenApi.Validations.Rules.OpenApiRule]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public void ValidateRequiredIsTrueWhenInIsPathInParameter()
};

// Act
var errors = parameter.Validate(ValidationRuleSet.GetDefaultRuleSet());

var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
validator.Enter("{name}");
var walker = new OpenApiWalker(validator);
walker.Walk(parameter);
var errors = validator.Errors;
// Assert
errors.Should().NotBeEmpty();
errors.Select(e => e.Message).Should().BeEquivalentTo(new[]
Expand Down Expand Up @@ -77,6 +80,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema()

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
validator.Enter("{parameter1}");
var walker = new OpenApiWalker(validator);
walker.Walk(parameter);

Expand All @@ -91,7 +95,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema()
});
errors.Select(e => e.Pointer).Should().BeEquivalentTo(new[]
{
"#/example",
"#/{parameter1}/example",
});
}

Expand Down Expand Up @@ -150,6 +154,7 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema()

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
validator.Enter("{parameter1}");
var walker = new OpenApiWalker(validator);
walker.Walk(parameter);

Expand All @@ -168,10 +173,83 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema()
{
// #enum/0 is not an error since the spec allows
// representing an object using a string.
"#/examples/example1/value/y",
"#/examples/example1/value/z",
"#/examples/example2/value"
"#/{parameter1}/examples/example1/value/y",
"#/{parameter1}/examples/example1/value/z",
"#/{parameter1}/examples/example2/value"
});
}

[Fact]
public void PathParameterNotInThePathShouldReturnAnError()
{
// Arrange
IEnumerable<OpenApiError> errors;

var parameter = new OpenApiParameter()
{
Name = "parameter1",
In = ParameterLocation.Path,
Required = true,
Schema = new OpenApiSchema()
{
Type = "string",
}
};

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());

var walker = new OpenApiWalker(validator);
walker.Walk(parameter);

errors = validator.Errors;
bool result = errors.Any();

// Assert
result.Should().BeTrue();
errors.OfType<OpenApiValidatorError>().Select(e => e.RuleName).Should().BeEquivalentTo(new[]
{
"PathParameterShouldBeInThePath"
});
errors.Select(e => e.Pointer).Should().BeEquivalentTo(new[]
{
"#/in"
});
}

[Fact]
public void PathParameterInThePastShouldBeOk()
{
// Arrange
IEnumerable<OpenApiError> errors;

var parameter = new OpenApiParameter()
{
Name = "parameter1",
In = ParameterLocation.Path,
Required = true,
Schema = new OpenApiSchema()
{
Type = "string",
}
};

// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
validator.Enter("paths");
validator.Enter("/{parameter1}");
validator.Enter("get");
validator.Enter("parameters");
validator.Enter("1");

var walker = new OpenApiWalker(validator);
walker.Walk(parameter);

errors = validator.Errors;
bool result = errors.Any();

// Assert
result.Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void DefaultRuleSetPropertyReturnsTheCorrectRules()
Assert.NotEmpty(rules);

// Update the number if you add new default rule(s).
Assert.Equal(20, rules.Count);
Assert.Equal(21, rules.Count);
}
}
}