Skip to content

Changes on "minProperties" / "maxProperties" are not detected #754

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
Feb 28, 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 @@ -78,7 +78,11 @@ public <V extends Schema<X>, X> DeferredChanged<ChangedSchema> diff(
.setExamples(new ChangedExamples(left.getExamples(), right.getExamples()))
.setExample(new ChangedExample(left.getExample(), right.getExample()))
.setMaxItems(new ChangedMaxItems(left.getMaxItems(), right.getMaxItems(), context))
.setMinItems(new ChangedMinItems(left.getMinItems(), right.getMinItems(), context));
.setMinItems(new ChangedMinItems(left.getMinItems(), right.getMinItems(), context))
.setMaxProperties(
new ChangedMaxProperties(left.getMaxProperties(), right.getMaxProperties(), context))
.setMinProperties(
new ChangedMinProperties(left.getMinProperties(), right.getMinProperties(), context));
builder
.with(
openApiDiff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.openapitools.openapidiff.core.model.schema.ChangedEnum;
import org.openapitools.openapidiff.core.model.schema.ChangedMaxItems;
import org.openapitools.openapidiff.core.model.schema.ChangedMaxLength;
import org.openapitools.openapidiff.core.model.schema.ChangedMaxProperties;
import org.openapitools.openapidiff.core.model.schema.ChangedMinItems;
import org.openapitools.openapidiff.core.model.schema.ChangedMinProperties;
import org.openapitools.openapidiff.core.model.schema.ChangedMultipleOf;
import org.openapitools.openapidiff.core.model.schema.ChangedNullable;
import org.openapitools.openapidiff.core.model.schema.ChangedNumericRange;
Expand Down Expand Up @@ -45,6 +47,8 @@ public class ChangedSchema implements ComposedChanged {
protected ChangedMultipleOf multipleOf;
protected ChangedMaxItems maxItems;
protected ChangedMinItems minItems;
protected ChangedMaxProperties maxProperties;
protected ChangedMinProperties minProperties;
protected ChangedNullable nullable;
protected boolean discriminatorPropertyChanged;
protected ChangedSchema items;
Expand Down Expand Up @@ -131,6 +135,8 @@ public List<Changed> getChangedElements() {
multipleOf,
maxItems,
minItems,
maxProperties,
minProperties,
nullable,
extensions))
.collect(Collectors.toList());
Expand Down Expand Up @@ -327,6 +333,14 @@ public ChangedExtensions getExtensions() {
return this.extensions;
}

public ChangedMaxProperties getMaxProperties() {
return this.maxProperties;
}

public ChangedMinProperties getMinProperties() {
return this.minProperties;
}

public ChangedSchema setContext(final DiffContext context) {
this.context = context;
return this;
Expand Down Expand Up @@ -503,6 +517,18 @@ public ChangedSchema setExtensions(final ChangedExtensions extensions) {
return this;
}

public ChangedSchema setMaxProperties(final ChangedMaxProperties maxProperties) {
clearChangedCache();
this.maxProperties = maxProperties;
return this;
}

public ChangedSchema setMinProperties(final ChangedMinProperties minProperties) {
clearChangedCache();
this.minProperties = minProperties;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -537,7 +563,9 @@ public boolean equals(Object o) {
&& Objects.equals(items, that.items)
&& Objects.equals(oneOfSchema, that.oneOfSchema)
&& Objects.equals(addProp, that.addProp)
&& Objects.equals(extensions, that.extensions);
&& Objects.equals(extensions, that.extensions)
&& Objects.equals(maxProperties, that.maxProperties)
&& Objects.equals(minProperties, that.minProperties);
}

@Override
Expand Down Expand Up @@ -572,7 +600,9 @@ public int hashCode() {
items,
oneOfSchema,
addProp,
extensions);
extensions,
maxProperties,
minProperties);
}

@java.lang.Override
Expand Down Expand Up @@ -637,6 +667,10 @@ public java.lang.String toString() {
+ this.getAddProp()
+ ", extensions="
+ this.getExtensions()
+ ", maxProperties="
+ this.getMaxProperties()
+ ", minProperties="
+ this.getMinProperties()
+ ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.openapitools.openapidiff.core.model.schema;

import org.openapitools.openapidiff.core.model.Changed;
import org.openapitools.openapidiff.core.model.DiffContext;
import org.openapitools.openapidiff.core.model.DiffResult;

public class ChangedMaxProperties implements Changed {
private Integer oldValue;
private Integer newValue;
private DiffContext context;

public ChangedMaxProperties(Integer oldValue, Integer newValue, DiffContext context) {
this.oldValue = oldValue;
this.newValue = newValue;
this.context = context;
}

public Integer getOldValue() {
return oldValue;
}

public Integer getNewValue() {
return newValue;
}

@Override
public DiffResult isChanged() {
if (oldValue == null && newValue == null) {
return DiffResult.NO_CHANGES;
}

if (oldValue == null) {
return DiffResult.INCOMPATIBLE;
}

if (newValue == null) {
return DiffResult.COMPATIBLE;
}

if (!oldValue.equals(newValue)) {
return oldValue > newValue ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
}

return DiffResult.NO_CHANGES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.openapitools.openapidiff.core.model.schema;

import org.openapitools.openapidiff.core.model.Changed;
import org.openapitools.openapidiff.core.model.DiffContext;
import org.openapitools.openapidiff.core.model.DiffResult;

public class ChangedMinProperties implements Changed {
private Integer oldValue;
private Integer newValue;
private DiffContext context;

public ChangedMinProperties(Integer oldValue, Integer newValue, DiffContext context) {
this.oldValue = oldValue;
this.newValue = newValue;
this.context = context;
}

public Integer getOldValue() {
return oldValue;
}

public Integer getNewValue() {
return newValue;
}

@Override
public DiffResult isChanged() {
if (oldValue == null && newValue == null) {
return DiffResult.NO_CHANGES;
}

if (oldValue == null) {
return DiffResult.INCOMPATIBLE;
}

if (newValue == null) {
return DiffResult.COMPATIBLE;
}

if (!oldValue.equals(newValue)) {
return newValue > oldValue ? DiffResult.INCOMPATIBLE : DiffResult.COMPATIBLE;
}

return DiffResult.NO_CHANGES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,39 @@ public void changeNullabeHandling() {
assertThat(props.get("field3").getNullable().getLeft()).isNull();
assertThat(props.get("field3").getNullable().getRight()).isTrue();
}

@Test // issue #479
public void changeMinMaxPropertiesHandling() {
ChangedOpenApi changedOpenApi =
OpenApiCompare.fromLocations(
"schemaDiff/schema-min-max-properties-diff-1.yaml",
"schemaDiff/schema-min-max-properties-diff-2.yaml");
ChangedSchema changedSchema =
getRequestBodyChangedSchema(
changedOpenApi, POST, "/schema/object/min-max-properties", "application/json");

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

// Check increasing of minProperties
assertThat(props.get("field1").getMinProperties().isIncompatible()).isTrue();
assertThat(props.get("field1").getMinProperties().getOldValue()).isEqualTo(1);
assertThat(props.get("field1").getMinProperties().getNewValue()).isEqualTo(10);

// Check decreasing of minProperties
assertThat(props.get("field2").getMinProperties().isCompatible()).isTrue();
assertThat(props.get("field2").getMinProperties().getOldValue()).isEqualTo(10);
assertThat(props.get("field2").getMinProperties().getNewValue()).isEqualTo(1);

// Check increasing of maxProperties
assertThat(props.get("field3").getMaxProperties().isCompatible()).isTrue();
assertThat(props.get("field3").getMaxProperties().getOldValue()).isEqualTo(10);
assertThat(props.get("field3").getMaxProperties().getNewValue()).isEqualTo(100);

// Check decreasing of maxProperties
assertThat(props.get("field4").getMaxProperties().isIncompatible()).isTrue();
assertThat(props.get("field4").getMaxProperties().getOldValue()).isEqualTo(100);
assertThat(props.get("field4").getMaxProperties().getNewValue()).isEqualTo(10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.1
info:
description: Schema diff
title: schema diff
version: 1.0.0
paths:
/schema/object/min-max-properties:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestDTO'
components:
schemas:
TestDTO:
type: object
properties:
field1:
type: object
minProperties: 1
field2:
type: object
minProperties: 10
field3:
type: object
maxProperties: 10
field4:
type: object
maxProperties: 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.1
info:
description: Schema diff
title: schema diff
version: 1.0.0
paths:
/schema/object/min-max-properties:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestDTO'
components:
schemas:
TestDTO:
type: object
properties:
field1:
type: object
minProperties: 10
field2:
type: object
minProperties: 1
field3:
type: object
maxProperties: 100
field4:
type: object
maxProperties: 10