openapi-style-validator icon indicating copy to clipboard operation
openapi-style-validator copied to clipboard

base64 type properties fail validation for lack of example

Open shoffmeister opened this issue 4 years ago • 8 comments

Whenever an object has a property of base64 (i.e. type: string, format: byte) validation will fail the error message

*ERROR* in Model '...', property 'content', field 'example' -> This field should be present and not empty

although the field example is present. This is really tied to the property being of type base64; remove the "format: byte" and the specification document will pass validation.

To reproduce, download the attached Zip file y.zip which contains two files minimally reproducing:

  • pom.xml
  • broken-validation-base64-response.yaml

Run mvn verify

//exp: passes validation //act: ERROR in Model

shoffmeister avatar Oct 31 '20 08:10 shoffmeister

@shoffmeister Thanks for the bug report! I'm not sure when I'll be able to look at this, I'm very busy for the next 2 weeks but if you send me a PR, I will try to review it quickly and make sure we do a release. Otherwise, it might take a couple of weeks.

Thanks!

JFCote avatar Nov 02 '20 12:11 JFCote

I made a quick test, it seems that the parser does not set the example property for the "byte" format.

  • A test that use the parser fail: https://github.com/gervaisb/openapi-style-validator/blob/e5fe49551c8103856dacd632948dd4c91019b9ae/cli/src/test/java/org/openapitools/openapistylevalidator/cli/MainTest.java#L320
  • Another that use a "hand made" OpenApi pass: https://github.com/gervaisb/openapi-style-validator/blob/e5fe49551c8103856dacd632948dd4c91019b9ae/lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java#L262

gervaisb avatar Nov 25 '21 20:11 gervaisb

@gervaisb So either it's a bug with swagger-parser that we use under the hood or swagger-parser follow specification and a byte field should not have example. I can't find any specific information about the byte field examples here: https://swagger.io/docs/specification/adding-examples/

If it's a bug: We need to create a bug in their project. https://github.com/swagger-api/swagger-parser

If it's meant to be like that and follow spec, we need to create a special validation that when the field is "byte", we don't count it as an error when there are no examples.

I don't have time right now to dig deeper but if you have some time, you could go and create an issue in their project since you already have examples.

JFCote avatar Nov 26 '21 13:11 JFCote

It seems to be a bug on the parser that tries to cast the example to the wrong type: https://github.com/swagger-api/swagger-parser/issues/1630#issuecomment-983017403

gervaisb avatar Nov 30 '21 21:11 gervaisb

I have the same issue here. As temporary workaround I use format: base64 with additional pattern: '^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'.

tymonx avatar Dec 27 '21 21:12 tymonx

@gervaisb and @tymonx. As soon as swagger-parser release a new version with this fix, I will update our dependency and create a new release.

Thanks for the workaround in the meantime! :)

JFCote avatar Jan 03 '22 12:01 JFCote

same seems to be true with format: binary

acabarbaye avatar Feb 28 '22 16:02 acabarbaye

Hello there,

The parser has been patched, it will now automatically get the bytes of a string. However, there are some implications with this fix, see this comment in the issue: https://github.com/swagger-api/swagger-parser/issues/1630#issuecomment-1076288181 We should be able to fix that issue as soon as they release that fix.

However, regarding the opinionated issues mentioned in the linked comment, should we not support the new swaggerParserBinaryAsString property too?

gervaisb avatar Mar 29 '22 09:03 gervaisb