vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 536 forks source link

Provide MediaType constants #1824

Open gsmet opened 3 years ago

gsmet commented 3 years ago

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!

pmlopes commented 3 years ago

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 commented 3 years ago

@vietj wdyt?

vietj commented 3 years ago

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?

pmlopes commented 3 years ago

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.

gsmet commented 3 years ago

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

pmlopes commented 3 years ago

@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

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

gsmet commented 3 years ago

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

gsmet commented 3 years ago

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