Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Add CSV mapper for REST server #606

Open
mathieu-lavigne opened this issue Oct 20, 2017 · 4 comments
Open

Add CSV mapper for REST server #606

mathieu-lavigne opened this issue Oct 20, 2017 · 4 comments

Comments

@mathieu-lavigne
Copy link
Contributor

mathieu-lavigne commented Oct 20, 2017

A default JSON mapper is already bundled with OASP4J. It may also include a default CSV mapper.

This CSV mapper should :

  • Allow sending CSV content to the client based on Eto
  • Allow receiving CSV content from the client and parse it to Eto
  • Detect CSV content based on JAXB annotations @Produces and @Consumes on the targeted API

CSV format

HTTP Headers

What we send

Content-Type: text/csv

We are sending CSV.

Content-Columns: col1, col2

Order and column names (as seen by Jackson). It should correspond to Eto fields.
Column name can differ from field name with @JsonProperty annotation.

If not specified client should include column names at the first line of its body.

Content-Column-Separator: ;

CSV default separator is , but client may use another one.

If not specified server should use , separator.

Content-Quote-Char: "

CSV default quote char is " but client may use another one.

If not specified server should use " quote char.

What we want to receive

Accept: text/csv

We want to receive CSV.

Accept-Columns: col1, col3

Order and column names we want to receive. See Content-Columns.
The Content-Columns of the server response must exactly match this client header.

If not specified server may use whatever columns it wants to include in its response.

Accept-Column-Separator: ;

Same as Content-Column-Separator when we want the server to use a specific CSV separator.

If not specified server should use , separator or server should include a correct Content-Column-Separator to specify which separator is used.

Accept-Quote-Char: "

Same as Content-Quote-Char when we want the server to use a specific CSV quote character.

If not specified server should use " quote character or server should include a correct Content-Quote-Char to specify which separator is used.

CsvFormat Object

Here is what can be configured :

  • withColumns : output only the given columns
  • withNullValue : change the output value for null
  • withHeader : output CSV header (first line with columns names)
  • withColumnSeparator : change default ',' CSV separator
  • withLineSeparator : set line feed format (Unix, Windows or System)
  • withEndingLineSeparator : output CSV with an empty line at the end
  • withQuoteChar : change default " CSV quote character (for escaping line feed for instance)
  • withCharset/withEncoding : change output encoding
  • withDateFormat : change the way dates are written into CSV

Usage

You can create a default CSV Format :

final CsvFormat base = new CsvFormat();

And create another one based on this one :

final CsvFormat format = base.withColumns("code,comment").withNullValue("");

Then we may read some CSV content :

final File file = new File("...");
final Eto readEto = format.readValue(file, Eto.class); // Eto class annotated with @JsonFilter(CsvFormat.FILTER)

Or write Eto to a CSV file :

final File out = new File("...");
format.writeValue(out, eto);
mathieu-lavigne added a commit to mathieu-lavigne/oasp4j that referenced this issue Nov 24, 2017
mathieu-lavigne added a commit to mathieu-lavigne/oasp4j that referenced this issue Dec 1, 2017
Remove sample ThreadLocals
mathieu-lavigne added a commit to mathieu-lavigne/oasp4j that referenced this issue Dec 4, 2017
@hohwille
Copy link
Member

hohwille commented Dec 5, 2017

Just stumbled over your work here. Nice feature enhancement. I wanted to suggest that you put that code to a separate module but as it seems you already did all that (csv module) so it perfectly fits to the new opt-in strategy we are following.
Happily awaiting your PR when you are done 👍

@hohwille
Copy link
Member

hohwille commented Dec 5, 2017

BTW: Are you also proposing this for large arrays to reduce overhead (we discovered some bandwidth issues for larger array datasets with JSON as all the property names are redundantly repeated). As it seems your solution is based on jackson and therefore perfectly integrated. Excited to see this coming...

@mathieu-lavigne
Copy link
Contributor Author

Hi Jörg and thanks for your encouraging comments !

This feature was intended to filter out columns we don't want to see in a CSV file. In fact Jackson parses every property and this may throw exceptions if the property is a composite object. The CSV values must always be simple object like Strings.

Indeed it seems it can be used to reduce overhead for large arrays but it hasn't been tested this way.

@mathieu-lavigne
Copy link
Contributor Author

mathieu-lavigne commented Dec 5, 2017

The PR has been created here

Still I do not understand why the build does not work...

Edit : resolved by ignoring not implemented test CsvProviderTest

mathieu-lavigne added a commit to mathieu-lavigne/oasp4j that referenced this issue Dec 5, 2017
Ignore not implemented test
mathieu-lavigne added a commit to mathieu-lavigne/oasp4j that referenced this issue Jan 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants