Skip to content

Doesn't take pattern field into account #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import org.openapitools.openapidiff.core.model.schema.*;
import org.openapitools.openapidiff.core.model.schema.ChangedMaxItems;
import org.openapitools.openapidiff.core.model.schema.ChangedMinItems;
import org.openapitools.openapidiff.core.model.schema.ChangedPattern;
import org.openapitools.openapidiff.core.model.schema.ChangedReadOnly;
import org.openapitools.openapidiff.core.model.schema.ChangedRequired;
import org.openapitools.openapidiff.core.model.schema.ChangedUniqueItems;

public class SchemaDiffResult {
protected ChangedSchema changedSchema;
Expand Down Expand Up @@ -75,6 +79,7 @@ public <V extends Schema<X>, X> DeferredChanged<ChangedSchema> diff(
context))
.setMultipleOf(new ChangedMultipleOf(left.getMultipleOf(), right.getMultipleOf()))
.setNullable(new ChangedNullable(left.getNullable(), right.getNullable()))
.setPattern(new ChangedPattern(left.getPattern(), right.getPattern(), context))
.setUniqueItems(new ChangedUniqueItems(left.getUniqueItems(), right.getUniqueItems()))
.setExamples(new ChangedExamples(left.getExamples(), right.getExamples()))
.setExample(new ChangedExample(left.getExample(), right.getExample()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import static org.openapitools.openapidiff.core.model.BackwardIncompatibleProp.SCHEMA_TYPE_CHANGED;

import io.swagger.v3.oas.models.media.Schema;
import java.util.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openapitools.openapidiff.core.model.schema.ChangedEnum;
Expand All @@ -18,6 +22,7 @@
import org.openapitools.openapidiff.core.model.schema.ChangedNullable;
import org.openapitools.openapidiff.core.model.schema.ChangedNumericRange;
import org.openapitools.openapidiff.core.model.schema.ChangedOneOfSchema;
import org.openapitools.openapidiff.core.model.schema.ChangedPattern;
import org.openapitools.openapidiff.core.model.schema.ChangedReadOnly;
import org.openapitools.openapidiff.core.model.schema.ChangedRequired;
import org.openapitools.openapidiff.core.model.schema.ChangedUniqueItems;
Expand Down Expand Up @@ -53,6 +58,7 @@ public class ChangedSchema implements ComposedChanged {
protected ChangedNullable nullable;
protected ChangedUniqueItems uniqueItems;
protected boolean discriminatorPropertyChanged;
protected ChangedPattern pattern;
protected ChangedSchema items;
protected ChangedOneOfSchema oneOfSchema;
protected ChangedSchema addProp;
Expand Down Expand Up @@ -141,6 +147,7 @@ public List<Changed> getChangedElements() {
minProperties,
nullable,
uniqueItems,
pattern,
extensions))
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -320,6 +327,10 @@ public ChangedUniqueItems getUniqueItems() {
return uniqueItems;
}

public ChangedPattern getPattern() {
return pattern;
}

public boolean isDiscriminatorPropertyChanged() {
return this.discriminatorPropertyChanged;
}
Expand Down Expand Up @@ -500,6 +511,12 @@ public ChangedSchema setUniqueItems(ChangedUniqueItems uniqueItems) {
return this;
}

public ChangedSchema setPattern(ChangedPattern pattern) {
clearChangedCache();
this.pattern = pattern;
return this;
}

public ChangedSchema setDiscriminatorPropertyChanged(final boolean discriminatorPropertyChanged) {
clearChangedCache();
this.discriminatorPropertyChanged = discriminatorPropertyChanged;
Expand Down Expand Up @@ -579,6 +596,7 @@ public boolean equals(Object o) {
&& Objects.equals(addProp, that.addProp)
&& Objects.equals(extensions, that.extensions)
&& Objects.equals(maxProperties, that.maxProperties)
&& Objects.equals(pattern, that.pattern)
&& Objects.equals(minProperties, that.minProperties);
}

Expand Down Expand Up @@ -617,6 +635,7 @@ public int hashCode() {
addProp,
extensions,
maxProperties,
pattern,
minProperties);
}

Expand Down Expand Up @@ -688,6 +707,8 @@ public java.lang.String toString() {
+ this.getMaxProperties()
+ ", minProperties="
+ this.getMinProperties()
+ ", pattern="
+ this.getPattern()
+ ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.openapitools.openapidiff.core.model.schema;

import java.util.Objects;
import org.openapitools.openapidiff.core.model.Changed;
import org.openapitools.openapidiff.core.model.DiffContext;
import org.openapitools.openapidiff.core.model.DiffResult;

public class ChangedPattern implements Changed {
private final String oldPattern;
private final String newPattern;
private final DiffContext context;

public ChangedPattern(String oldPattern, String newPattern, DiffContext context) {
this.oldPattern = oldPattern;
this.newPattern = newPattern;
this.context = context;
}

@Override
public DiffResult isChanged() {
return Objects.equals(oldPattern, newPattern) ? DiffResult.NO_CHANGES : DiffResult.INCOMPATIBLE;
}

@Override
public boolean isCompatible() {
return Objects.equals(oldPattern, newPattern);
}

public DiffResult isDiffBackwardCompatible() {
if (!Objects.equals(oldPattern, newPattern)) {
return DiffResult.INCOMPATIBLE;
}
return DiffResult.COMPATIBLE;
}

public String getOldPattern() {
return oldPattern;
}

public String getNewPattern() {
return newPattern;
}

public DiffContext getContext() {
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,40 @@ public void requiredPropertyChangedTest() {
// Verify breaking changes in response - removing required properties is breaking
assertThat(responseRequired.isItemsChanged()).isEqualTo(DiffResult.INCOMPATIBLE);
}

@Test // issue #104
public void changePatternHandling() {
ChangedOpenApi changedOpenApi =
OpenApiCompare.fromLocations(
"schemaDiff/schema-pattern-diff-1.yaml", "schemaDiff/schema-pattern-diff-2.yaml");
ChangedSchema changedSchema =
getRequestBodyChangedSchema(changedOpenApi, POST, "/schema/pattern", "application/json");

assertThat(changedSchema).isNotNull();
Map<String, ChangedSchema> props = changedSchema.getChangedProperties();
assertThat(props).isNotEmpty();

// Check no changes in pattern
assertThat(props.get("patternUnchanged")).isNull();

// Check changes in pattern (modification)
assertThat(props.get("patternModified").getPattern().isChanged())
.isEqualTo(DiffResult.INCOMPATIBLE);
assertThat(props.get("patternModified").getPattern().getOldPattern()).isEqualTo("^[a-z]+$");
assertThat(props.get("patternModified").getPattern().getNewPattern()).isEqualTo("^[a-zA-Z]+$");

// Check deletion of pattern
assertThat(props.get("patternRemoved").getPattern().isChanged())
.isEqualTo(DiffResult.INCOMPATIBLE);
assertThat(props.get("patternRemoved").getPattern().getOldPattern()).isEqualTo("^[0-9]+$");
assertThat(props.get("patternRemoved").getPattern().getNewPattern()).isNull();

// Check addition of pattern
assertThat(props.get("patternAdded").getPattern().isChanged())
.isEqualTo(DiffResult.INCOMPATIBLE);
assertThat(props.get("patternAdded").getPattern().getOldPattern()).isNull();
// Adjust assertion to expect single backslashes as parsed from YAML
assertThat(props.get("patternAdded").getPattern().getNewPattern())
.isEqualTo("^\\d{3}-\\d{2}-\\d{4}$?");
}
}
24 changes: 24 additions & 0 deletions core/src/test/resources/schemaDiff/schema-pattern-diff-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Schema Pattern Diff Test - Old
version: 1.0.0
paths:
/schema/pattern:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
patternUnchanged:
type: string
pattern: "^[a-z]+$"
patternModified:
type: string
pattern: "^[a-z]+$"
patternRemoved:
type: string
pattern: "^[0-9]+$"
patternAdded:
type: string
24 changes: 24 additions & 0 deletions core/src/test/resources/schemaDiff/schema-pattern-diff-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Schema Pattern Diff Test - New
version: 1.0.0
paths:
/schema/pattern:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
patternUnchanged:
type: string
pattern: "^[a-z]+$"
patternModified:
type: string
pattern: "^[a-zA-Z]+$"
patternRemoved:
type: string
patternAdded:
type: string
pattern: "^\\d{3}-\\d{2}-\\d{4}$?"