Java源码示例:io.swagger.v3.oas.models.media.ByteArraySchema
示例1
/**
* Decodes the given string and returns an object applicable to the given schema.
* Throws a ParseException if no applicable object can be recognized.
*/
private Object getDecodedObject( Schema schema, String objectString) throws ParseException {
Object object =
objectString == null?
null :
schema.getClass().equals( DateSchema.class)?
toDate( objectString) :
schema.getClass().equals( DateTimeSchema.class)?
toDateTime( objectString) :
schema.getClass().equals( ByteArraySchema.class)?
toBytes( objectString) :
objectString;
if( object == null && objectString != null) {
throw new ParseException( objectString, 0);
}
return object;
}
示例2
@Test
public void testByte() {
MetaPrimitiveType type = new MetaPrimitiveType();
type.setName("byte");
Schema schema = OASUtils.transformMetaResourceField(type);
Assert.assertTrue(schema instanceof ByteArraySchema);
}
示例3
@Test
public void testIssue75() {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
final OpenAPI openAPI = parser.read("src/test/resources/issue99.yaml");
RequestBody body = openAPI.getPaths().get("/albums").getPost().getRequestBody();
Schema model = body.getContent().get("application/json").getSchema();
assertNotNull(model);
assertTrue(model instanceof ArraySchema);
ArraySchema am = (ArraySchema) model;
assertTrue(am.getItems() instanceof ByteArraySchema);
assertEquals(am.getItems().getFormat(), "byte");
}
示例4
public static Schema createSchema(String type, String format) {
if(INTEGER_TYPE.equals(type)) {
if(StringUtils.isBlank(format)){
return new IntegerSchema().format(null);
}else {
return new IntegerSchema().format(format);
}
}
else if(NUMBER_TYPE.equals(type)) {
if (StringUtils.isBlank(format)){
return new NumberSchema();
} else {
return new NumberSchema().format(format);
}
}
else if(BOOLEAN_TYPE.equals(type)) {
if (StringUtils.isBlank(format)){
return new BooleanSchema();
} else {
return new BooleanSchema().format(format);
}
}
else if(STRING_TYPE.equals(type)) {
if(BYTE_FORMAT.equals(format)) {
return new ByteArraySchema();
}
else if(BINARY_FORMAT.equals(format)) {
return new BinarySchema();
}
else if(DATE_FORMAT.equals(format)) {
return new DateSchema();
}
else if(DATE_TIME_FORMAT.equals(format)) {
return new DateTimeSchema();
}
else if(PASSWORD_FORMAT.equals(format)) {
return new PasswordSchema();
}
else if(EMAIL_FORMAT.equals(format)) {
return new EmailSchema();
}
else if(UUID_FORMAT.equals(format)) {
return new UUIDSchema();
}
else {
if (StringUtils.isBlank(format)){
return new StringSchema().format(null);
}else {
return new StringSchema().format(format);
}
}
}
else if(OBJECT_TYPE.equals(type)) {
return new ObjectSchema();
}
else {
return new Schema();
}
}
示例5
@Test
public void testDeserializeByteString() {
String yaml = "openapi: 3.0.0\n" +
"servers: []\n" +
"info:\n" +
" version: 0.0.0\n" +
" title: My Title\n" +
"paths:\n" +
" /persons:\n" +
" get:\n" +
" description: a test\n" +
" responses:\n" +
" '200':\n" +
" description: Successful response\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" type: object\n" +
" properties:\n" +
" bytes:\n" +
" $ref: '#/components/schemas/ByteString'\n" +
"components:\n" +
" schemas:\n" +
" ByteString:\n" +
" type: string\n" +
" format: byte\n" +
" default: W.T.F?\n" +
" enum:\n" +
" - VGhlIHdvcmxk\n" +
" - aXMgYWxs\n" +
" - dGhhdCBpcw==\n" +
" - dGhlIGNhc2U=\n" +
" - W.T.F?\n" +
"";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
Schema byteModel = resolved.getComponents().getSchemas().get("ByteString");
assertTrue(byteModel instanceof ByteArraySchema);
List<byte[]> byteValues = byteModel.getEnum();
assertEquals(byteValues.size(), 4);
assertEquals(new String( byteValues.get(0)), "The world");
assertEquals(new String( byteValues.get(1)), "is all");
assertEquals(new String( byteValues.get(2)), "that is");
assertEquals(new String( byteValues.get(3)), "the case");
assertEquals( byteModel.getDefault(), null);
assertEquals(
result.getMessages(),
Arrays.asList(
"attribute components.schemas.ByteString.enum=`W.T.F?` is not of type `byte`",
"attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
}