Skip to content
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

Equivalent paths with overloaded parameters are supported #306

Merged
merged 2 commits into from
Mar 3, 2022
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
@@ -1,10 +1,13 @@
package org.openapitools.openapidiff.core.compare;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.openapitools.openapidiff.core.model.Changed;
import org.openapitools.openapidiff.core.model.ChangedPaths;
import org.openapitools.openapidiff.core.model.DiffContext;
Expand Down Expand Up @@ -50,7 +53,7 @@ public DeferredChanged<ChangedPaths> diff(
.filter(item -> normalizePath(item.getKey()).equals(template))
.min(
(a, b) -> {
if (methodsIntersect(a.getValue(), b.getValue())) {
if (methodsAndParametersIntersect(a.getValue(), b.getValue())) {
throw new IllegalArgumentException(
"Two path items have the same signature: " + template);
}
Expand Down Expand Up @@ -99,13 +102,40 @@ public static Paths valOrEmpty(Paths path) {
return path;
}

private static boolean methodsIntersect(PathItem a, PathItem b) {
/**
*
* @param a a path form the open api spec
* @param b another path from the same open api spec
* @return <code>true</code> in case both paths are of the same method AND their templated parameters are of the same type;
* <code>false</code> otherwise
*
*/
private static boolean methodsAndParametersIntersect(PathItem a, PathItem b) {
Set<PathItem.HttpMethod> methodsA = a.readOperationsMap().keySet();
for (PathItem.HttpMethod method : b.readOperationsMap().keySet()) {
if (methodsA.contains(method)) {
return true;
Operation left = a.readOperationsMap().get(method);
Operation right = b.readOperationsMap().get(method);
if (left.getParameters().size() == right.getParameters().size()) {
return parametersIntersect(left.getParameters(), right.getParameters());
}
return false;
}
}
return false;
}

/**
*
* @param left parameters from the first compared method
* @param right parameters from the second compared method
* @return <code>true</code> in case each parameter pair is of the same type; <code>false</code> otherwise
*/
private static boolean parametersIntersect(List<Parameter> left, List<Parameter> right) {;
int parametersSize = left.size();
long intersectedParameters = IntStream.range(0, left.size())
.filter(i -> left.get(i).getSchema().getType().equals(right.get(i).getSchema().getType()))
.count();
return parametersSize == intersectedParameters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openapitools.openapidiff.core;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.openapitools.openapidiff.core.TestUtils.assertOpenApiAreEquals;

class ParametersOverloadingTest {

private final String OVERLOADED_PARAMETERS = "parameters_overloading.yaml";
private final String DUPLICATED_PARAMETER_TYPES = "parameters_overloading_2.yaml";

@Test
void testDiffWithOverloadedParameterTypes() {
assertDoesNotThrow(() -> OpenApiCompare.fromLocations(OVERLOADED_PARAMETERS, OVERLOADED_PARAMETERS));
assertOpenApiAreEquals(OVERLOADED_PARAMETERS, OVERLOADED_PARAMETERS);
}

@Test
void testDiffWithDuplicatedParameterTypes() {
assertThrows(
IllegalArgumentException.class,
() -> OpenApiCompare.fromLocations(DUPLICATED_PARAMETER_TYPES, DUPLICATED_PARAMETER_TYPES),
"Two path items have the same signature: /projects/{}");
}
}
52 changes: 52 additions & 0 deletions core/src/test/resources/parameters_overloading.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.0.2
info:
title: Projects API
version: 1.0.0
paths:
/projects/{id}:
get:
parameters:
- in: path
name: id
required: true
schema:
type: integer
format: int64
responses:
'200':
description: 'Success'
content:
application/json:
schema:
$ref: '#/components/schemas/SampleResponse'
/projects/{uid}:
get:
parameters:
- in: path
name: uid
required: true
schema:
type: string
format: uuid
responses:
'200':
description: 'Success'
content:
application/json:
schema:
$ref: '#/components/schemas/SampleResponse'
components:
schemas:
SampleResponse:
type: object
properties:
id:
type: integer
uid:
type: string
name:
type: string
required:
- id
- uid
- name
52 changes: 52 additions & 0 deletions core/src/test/resources/parameters_overloading_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.0.2
info:
title: Projects API
version: 1.0.0
paths:
/projects/{id}:
get:
parameters:
- in: path
name: id
required: true
schema:
type: integer
format: int64
responses:
'200':
description: 'Success'
content:
application/json:
schema:
$ref: '#/components/schemas/SampleResponse'
/projects/{uid}:
get:
parameters:
- in: path
name: uid
required: true
schema:
type: integer
format: int64
responses:
'200':
description: 'Success'
content:
application/json:
schema:
$ref: '#/components/schemas/SampleResponse'
components:
schemas:
SampleResponse:
type: object
properties:
id:
type: integer
uid:
type: string
name:
type: string
required:
- id
- uid
- name