-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
31 changed files
with
1,508 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3RefTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.swagger.v3.parser.test; | ||
|
||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.testng.Assert; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.Operation; | ||
import io.swagger.v3.oas.models.examples.Example; | ||
import io.swagger.v3.oas.models.media.MediaType; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import io.swagger.v3.parser.OpenAPIV3Parser; | ||
import io.swagger.v3.parser.core.models.ParseOptions; | ||
|
||
import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
|
||
public class OpenAPIV3RefTest { | ||
private OpenAPI oas; | ||
@Before | ||
public void parseOASSpec() { | ||
ParseOptions options = new ParseOptions(); | ||
options.setResolve(true); | ||
oas = new OpenAPIV3Parser().read("oas3-refs-test/openapi.json", null, options); | ||
} | ||
@Test | ||
public void testParameterExampleRefProcessed() { | ||
String paramName = "correlation_id"; | ||
Map<String, Example> paramExamples = oas.getComponents().getParameters().get(paramName).getExamples(); | ||
Assert.assertEquals(paramExamples.size(), 1, "Parameter has an example"); | ||
Assert.assertTrue(paramExamples.values() | ||
.stream().allMatch(e -> e.get$ref().equalsIgnoreCase("#/components/examples/" + paramName)), | ||
"Examples are referenced"); | ||
Assert.assertEquals(oas.getComponents().getExamples().get(paramName).getValue(), "7758b780aaaca", | ||
"Examples are processed"); | ||
} | ||
|
||
@Test | ||
public void testDiscriminatorMappingRefsUpdated() { | ||
Schema reqSchema = oas.getPaths().values().stream() | ||
.findFirst() | ||
.map(path -> path.getPost().getRequestBody().getContent().getOrDefault("application/json", null)) | ||
.map(MediaType::getSchema) | ||
.orElseThrow(() -> new IllegalStateException("Path not processed!")); | ||
Collection<String> discriminator = reqSchema.getDiscriminator().getMapping().values(); | ||
Set<String> allOfs = ((List<Schema>)reqSchema.getAnyOf()) | ||
.stream().map(Schema::get$ref).collect(Collectors.toSet()); | ||
assertTrue(allOfs.stream().allMatch(s -> s.contains(COMPONENTS_SCHEMAS_REF)),"Schema mappings are processed"); | ||
assertTrue(allOfs.containsAll(discriminator),"Discriminator mappings are updated"); | ||
|
||
} | ||
|
||
@Test | ||
public void testCallbackRef() { | ||
assertEquals(oas.getComponents().getCallbacks().size(), 1, "Callbacks processed"); | ||
Operation cbOperation = oas.getComponents().getCallbacks() | ||
.get("vaccination_complete") | ||
.get("{$request.body#/callback_url}/pets/{$request.path.id}/vaccinations").getPut(); | ||
assertNotNull(cbOperation); | ||
assertEquals(cbOperation.getRequestBody().getContent().get("application/json").getSchema().get$ref(), | ||
COMPONENTS_SCHEMAS_REF + "vaccination_record", "Callback Request body processed"); | ||
assertEquals(cbOperation.getResponses().get("400").getContent().get("application/json").getSchema().get$ref(), | ||
COMPONENTS_SCHEMAS_REF + "error" , "Callback responses Processed"); | ||
} | ||
|
||
@Test | ||
public void testComposedArrayItemsRef() { | ||
Schema adoptionRequest = oas.getComponents() | ||
.getSchemas().get("adoption_request"); | ||
assertEquals(adoptionRequest.getTitle(), "Adoption Request", "Processed ref Schemas"); | ||
assertEquals(((Schema) adoptionRequest.getProperties().get("adopter_alias")).get$ref(), | ||
COMPONENTS_SCHEMAS_REF + "alias_array", "Processed ref added to Schemas"); | ||
Schema adopterAlias = ((Schema) oas.getComponents().getSchemas().get("alias_array")); | ||
assertEquals(adopterAlias.getTitle(),"AdopterAlias", "Processed Schemas"); | ||
assertEquals(adopterAlias.getItems().getAllOf().size(), 2, "Processed schemas items"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...3-refs-test/components/callbacks/pets.id.order-vaccination/post/vaccination_complete.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{ | ||
"{$request.body#/callback_url}/pets/{$request.path.id}/vaccinations": { | ||
"put": { | ||
"summary": "Vaccination record sent on completion", | ||
"description": "New vaccination record for a pet sent after vaccination order is complete.", | ||
"x-slo": { | ||
"response_time_95th_percentile": 100, | ||
"error_rate": 0.001 | ||
}, | ||
"x-visibility": { | ||
"extent": "INTERNAL" | ||
}, | ||
"requestBody": { | ||
"required": true, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "../../../schemas/vaccination_record.json" | ||
}, | ||
"examples": { | ||
"vaccination": { | ||
"summary": "Vaccination with ID", | ||
"description": "Vaccination record for pet with vaccination id.", | ||
"value": { | ||
"id": "8ce1edf4-3014-4b5c-b50d-9ed697768ead", | ||
"vaccine_name": "Feline Viral Rhinotracheitis", | ||
"administration_date": "2023-12-20" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"204": { | ||
"description": "Your server implementation should return this HTTP status code if the data was received successfully." | ||
}, | ||
"400": { | ||
"description": "Your server should return this HTTP status code if the request is malformed.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "../../../schemas/error.json" | ||
}, | ||
"examples": { | ||
"bad_vaccine_name": { | ||
"summary": "Vaccine name is invalid.", | ||
"value": { | ||
"name": "INVALID_REQUEST", | ||
"debug_id": "b1d1f06c7246c", | ||
"message": "Request is not well-formed, syntactically incorrect, or violates schema.", | ||
"details": [ | ||
{ | ||
"field": "/vaccine_name", | ||
"location": "body", | ||
"value": "#bad-value#", | ||
"issue": "INVALID_PARAMETER_SYNTAX", | ||
"description": "The value of a field does not conform to the expected format." | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"404": { | ||
"description": "Your server should return this HTTP status code if the pet id is not found.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "../../../schemas/error.json" | ||
}, | ||
"examples": { | ||
"generic": { | ||
"summary": "Pet 'Not Found' error.", | ||
"value": { | ||
"name": "RESOURCE_NOT_FOUND", | ||
"debug_id": "b1d1f06c7246c", | ||
"message": "The specified resource does not exist." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"500": { | ||
"description": "Your server should return this HTTP status code if there is an internal error processing the request.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "../../../schemas/error.json" | ||
}, | ||
"examples": { | ||
"generic": { | ||
"summary": "Generic internal server error.", | ||
"value": { | ||
"name": "INTERNAL_SERVER_ERROR", | ||
"debug_id": "b1d1f06c7246c", | ||
"message": "An internal server error has occurred." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.