use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.
the class SwaggerConverter method convert.
public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
Parameter v3Parameter = new Parameter();
if (StringUtils.isNotBlank(v2Parameter.getDescription())) {
v3Parameter.setDescription(v2Parameter.getDescription());
}
if (v2Parameter instanceof SerializableParameter) {
v3Parameter.setAllowEmptyValue(((SerializableParameter) v2Parameter).getAllowEmptyValue());
}
v3Parameter.setIn(v2Parameter.getIn());
v3Parameter.setName(v2Parameter.getName());
Object exampleExtension = v2Parameter.getVendorExtensions().get("x-example");
if (exampleExtension != null) {
v3Parameter.setExample(exampleExtension);
}
Schema schema = null;
if (v2Parameter instanceof RefParameter) {
RefParameter ref = (RefParameter) v2Parameter;
if (ref.get$ref().indexOf("#/parameters") == 0) {
String updatedRef = "#/components/";
if (components.getRequestBodies() != null && components.getRequestBodies().get(ref.getSimpleRef()) != null) {
updatedRef += "requestBodies";
} else if (components.getSchemas() != null && components.getSchemas().get("formData_" + ref.getSimpleRef()) != null) {
updatedRef += "schemas";
} else {
updatedRef += "parameters";
}
updatedRef += ref.get$ref().substring("#/parameters".length());
ref.set$ref(updatedRef);
}
v3Parameter.set$ref(ref.get$ref());
} else if (v2Parameter instanceof SerializableParameter) {
SerializableParameter sp = (SerializableParameter) v2Parameter;
if ("array".equals(sp.getType())) {
ArraySchema a = new ArraySchema();
// TODO: convert arrays to proper template format
String cf = sp.getCollectionFormat();
if (StringUtils.isEmpty(cf)) {
cf = "csv";
}
switch(cf) {
case "ssv":
if ("query".equals(v2Parameter.getIn())) {
v3Parameter.setStyle(Parameter.StyleEnum.SPACEDELIMITED);
}
break;
case "pipes":
if ("query".equals(v2Parameter.getIn())) {
v3Parameter.setStyle((Parameter.StyleEnum.PIPEDELIMITED));
}
break;
case "tsv":
break;
case "multi":
if ("query".equals(v2Parameter.getIn())) {
v3Parameter.setStyle(Parameter.StyleEnum.FORM);
v3Parameter.setExplode(true);
}
break;
case "csv":
default:
if ("query".equals(v2Parameter.getIn())) {
v3Parameter.setStyle(Parameter.StyleEnum.FORM);
v3Parameter.setExplode(false);
} else if ("header".equals(v2Parameter.getIn()) || "path".equals(v2Parameter.getIn())) {
v3Parameter.setStyle(Parameter.StyleEnum.SIMPLE);
v3Parameter.setExplode(false);
}
}
Property items = sp.getItems();
Schema itemsSchema = convert(items);
a.setItems(itemsSchema);
if (sp.getMaxItems() != null) {
a.setMaxItems(sp.getMaxItems());
}
if (sp.getMinItems() != null) {
a.setMinItems(sp.getMinItems());
}
if (sp.isUniqueItems() != null) {
a.setUniqueItems(sp.isUniqueItems());
}
schema = a;
} else {
schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
schema.setType(sp.getType());
schema.setFormat(sp.getFormat());
schema.setMaximum(sp.getMaximum());
schema.setExclusiveMaximum(sp.isExclusiveMaximum());
schema.setMinimum(sp.getMinimum());
schema.setExclusiveMinimum(sp.isExclusiveMinimum());
schema.setMinLength(sp.getMinLength());
schema.setMaxLength(sp.getMaxLength());
if (sp.getMultipleOf() != null) {
schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
}
schema.setPattern(sp.getPattern());
}
if (sp.getEnum() != null) {
for (String e : sp.getEnum()) {
switch(sp.getType() == null ? SchemaTypeUtil.OBJECT_TYPE : sp.getType()) {
case SchemaTypeUtil.INTEGER_TYPE:
schema.addEnumItemObject(Integer.parseInt(e));
break;
case SchemaTypeUtil.NUMBER_TYPE:
schema.addEnumItemObject(new BigDecimal(e));
break;
case SchemaTypeUtil.BOOLEAN_TYPE:
schema.addEnumItemObject(Boolean.valueOf(e));
break;
default:
schema.addEnumItemObject(e);
break;
}
}
}
if (sp.getVendorExtensions() != null) {
Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
if (nullableExtension != null) {
schema.setNullable((Boolean) nullableExtension);
}
}
schema.setExtensions(convert(sp.getVendorExtensions()));
if (sp instanceof AbstractSerializableParameter) {
AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
schema.setDefault(ap.getDefault());
}
}
if (v2Parameter.getRequired()) {
v3Parameter.setRequired(v2Parameter.getRequired());
}
v3Parameter.setSchema(schema);
v3Parameter.setExtensions(convert(v2Parameter.getVendorExtensions()));
return v3Parameter;
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.
the class V2ConverterTest method testIssue785.
@Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
public void testIssue785() {
AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
List<AuthorizationValue> authorizationValues = Arrays.asList(apiKey);
SwaggerConverter converter = new SwaggerConverter();
List<io.swagger.models.auth.AuthorizationValue> convertedAuthList = converter.convert(authorizationValues);
assertEquals(convertedAuthList.size(), authorizationValues.size());
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-core by swagger-api.
the class JsonDeserializationTest method deserializeHeaderWithStyle.
@Test(description = "it should deserialize a Header with style")
public void deserializeHeaderWithStyle() throws IOException {
final String json = "{\"description\":\"aaaa\",\"style\":\"simple\"}";
final Header p = m.readValue(json, Header.class);
SerializationMatchers.assertEqualsToJson(p, json);
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-core by swagger-api.
the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS31.
@Test
public void testDeserializationOnOAS31() throws IOException {
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
assertEquals(openAPI.getInfo().getSummary(), "petstore sample for OAS 3.1.0");
assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test");
assertNotNull(openAPI.getWebhooks());
assertFalse(openAPI.getWebhooks().isEmpty());
assertNotNull(openAPI.getWebhooks().get("newPet"));
assertNotNull(openAPI.getWebhooks().get("newPet").getPost());
assertNotNull(openAPI.getComponents().getPathItems());
assertNotNull(openAPI.getComponents().getPathItems().get("/pet"));
assertEquals(openAPI.getComponents().getPathItems().get("/pet").getDescription(), "get a pet");
assertNotNull(openAPI.getComponents().getPathItems().get("/pet").getGet());
assertEquals(openAPI.getComponents().getPathItems().get("/pet").getGet().getOperationId(), "getPet");
assertNotNull(openAPI.getComponents().getSchemas());
assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions().get("x-test-extension"), "extended");
assertNotNull(openAPI.getComponents().getResponses());
assertNotNull(openAPI.getComponents().getResponses().get("201"));
assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
assertNotNull(openAPI.getComponents().getParameters());
assertNotNull(openAPI.getComponents().getParameters().get("param"));
assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
assertNotNull(openAPI.getComponents().getExamples());
assertNotNull(openAPI.getComponents().getExamples().get("example"));
assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
assertNotNull(openAPI.getComponents().getRequestBodies());
assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
assertNotNull(openAPI.getComponents().getHeaders());
assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
assertNotNull(openAPI.getComponents().getSecuritySchemes());
assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
assertNotNull(openAPI.getComponents().getLinks());
assertNotNull(openAPI.getComponents().getLinks().get("Link"));
assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
assertNotNull(openAPI.getComponents().getCallbacks());
assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
assertNull(openAPI.getInfo().getSummary());
assertNull(openAPI.getWebhooks());
assertNull(openAPI.getComponents().getPathItems());
assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
}
use of io.swagger.v3.oas.annotations.headers.Header in project swagger-core by swagger-api.
the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS30.
@Test
public void testDeserializationOnOAS30() throws IOException {
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
OpenAPI openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
assertNull(openAPI.getInfo().getSummary());
assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
assertNull(openAPI.getInfo().getLicense().getIdentifier());
assertNull(openAPI.getWebhooks());
assertNull(openAPI.getComponents().getPathItems());
assertNotNull(openAPI.getComponents().getSchemas());
assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
assertNotNull(openAPI.getComponents().getResponses());
assertNotNull(openAPI.getComponents().getResponses().get("201"));
assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
assertNotNull(openAPI.getComponents().getParameters());
assertNotNull(openAPI.getComponents().getParameters().get("param"));
assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
assertNotNull(openAPI.getComponents().getExamples());
assertNotNull(openAPI.getComponents().getExamples().get("example"));
assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
assertNotNull(openAPI.getComponents().getRequestBodies());
assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
assertNotNull(openAPI.getComponents().getHeaders());
assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
assertNotNull(openAPI.getComponents().getSecuritySchemes());
assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
assertNotNull(openAPI.getComponents().getLinks());
assertNotNull(openAPI.getComponents().getLinks().get("Link"));
assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
assertNotNull(openAPI.getComponents().getCallbacks());
assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
}
Aggregations