vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

Provide MediaType constants

Open gsmet opened this issue 4 years ago • 8 comments

Hi,

I'm using Reactive Routes in Quarkus which relies on Vert.x Web and, as someone familiar with JAX-RS, I miss my MediaType constants.

See https://github.com/jax-rs/api/blob/master/jaxrs-api/src/main/java/javax/ws/rs/core/MediaType.java#L77 . I'm talking about the String constants specifically so that I could use them when declaring @Route annotations (for consumes and produces).

Would it make sense to expose such a class in Vert.x Web?

Thanks!

gsmet avatar Dec 17 '20 12:12 gsmet

Internally in core there's the MimeMapping class that has many of these values. Perhaps an alternative suggestion would be to expose those values as constants?

pmlopes avatar Jan 12 '21 20:01 pmlopes

@vietj wdyt?

pmlopes avatar Jan 12 '21 20:01 pmlopes

it could be also achieve in reactive routes integration layer, so I think the concern is that beside using in reactive routes, would it make sense to have in vertx-web itself for users not using reactive routes?

vietj avatar Jan 13 '21 08:01 vietj

ok, @gsmet would an interface like:

@VertxGen
public interface MimeType {
  String APPLICATION_JSON = "application/json";
  ...
}

Be enough?

Using this approach, users can still use the string notation for custom types, for example: application/json+myextension or use the constant from the interface which avoids typpo kind of errors.

pmlopes avatar Jan 13 '21 09:01 pmlopes

Yes, I'm all to keep them simple strings.

gsmet avatar Jan 13 '21 09:01 gsmet

@gsmet, let's start small, I'll just dump the same list we have in core but as constants for now and users are welcome to do later pull requests for missing types.

Now the trick is name things. How should we encode the constant names, for example there are:

application/json
application/vnd.ms-excel
image/svg+xml

Should we just replace all not allowed variable name characters as _ ?

APPLICATION_JSON
APPLICATION_VND_MS_EXCEL
IMAGE_SVG_XML

Or have a group like the IANA registry: http://www.iana.org/assignments/media-types/media-types.xhtml

  • application
  • audio
  • font
  • image
  • message
  • model
  • multipart
  • text
  • video

Though inner classes/interfaces are known to cause trouble with codegen.

pmlopes avatar Jan 13 '21 14:01 pmlopes

I would keep it simple and have an approach similar to the RESTEasy code I posted above.

gsmet avatar Jan 13 '21 14:01 gsmet

Also I think having the ones listed in RESTEasy would probably be a good start. No need for all of them.

gsmet avatar Jan 13 '21 14:01 gsmet