Skip to content

Proposal for java code generation of **format: time** field declaration #166

@jmzc

Description

@jmzc
Description

Proposal for java code generation of format: time field declaration

openapi-generator version

3.0.0-SNAPSHOT

OpenAPI declaration file content or url
hour:
          type: string
          example: "11:00+02:00"
          format: time
Command line used for generation
Suggest a fix/enhancement

I changed the data type of fields in model classes from String to OffsetTime

I modified JSON.java class with the next code

private OffsetTimeTypeAdapter offsetTimeTypeAdapter = new OffsetTimeTypeAdapter();
 public JSON() {

 gson = createGson()
            .registerTypeAdapter(OffsetTime.class, offsetTimeTypeAdapter)
            .create();
}
public class OffsetTimeTypeAdapter extends TypeAdapter<OffsetTime> {

        private DateTimeFormatter formatter;

        public OffsetTimeTypeAdapter() {
            this(DateTimeFormatter.ISO_OFFSET_TIME);
        }

        public OffsetTimeTypeAdapter(DateTimeFormatter formatter) {
            this.formatter = formatter;
        }

        public void setFormat(DateTimeFormatter dateFormat) {
            this.formatter = dateFormat;
        }

        @Override
        public void write(JsonWriter out, OffsetTime date) throws IOException {
            if (date == null) {
                out.nullValue();
            } else {
                out.value(formatter.format(date));
            }
        }

        @Override
        public OffsetTime read(JsonReader in) throws IOException {
            switch (in.peek()) {
                case NULL:
                    in.nextNull();
                    return null;
                default:
                    String date = in.nextString();
                    return OffsetTime.parse(date, formatter);
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions