poem-web / poem

A full-featured and easy-to-use web framework with the Rust programming language.
Apache License 2.0
3.65k stars 293 forks source link

Openapi: wrong format for unsigned integers #760

Open DDtKey opened 9 months ago

DDtKey commented 9 months ago

Expected Behavior

#[derive(Debug, Object, Clone)]
struct MyStructure {
    a: u64,
    b: u16,
}

Should produce openapi-schema with minimum & maximum instead of uint16/32/64.

MyStructure: 
      type: object
      properties:
        a:
          type: integer
          minimum: 0
        b:
          type: integer
          minimum: 0
          maximum:  65535

It's not unified format and usually controlled by minimum / maximum & exclusiveMinimum Exists only one unsigned (unified) definition: uint8

Actual Behavior

Currently it produces

MyStructure: 
      type: object
      properties:
        a:
          type: integer
          format: uint64
        b:
          type: integer
          format: uint16
sunli829 commented 8 months ago

As per the JSON Schema spec format is an open-ended keyword, so you can define your own values.

https://github.com/OAI/OpenAPI-Specification/issues/2617

DDtKey commented 8 months ago

Hm, right, that makes sense in terms of OAI spec. Thanks for the reference So I guess format can be preserved, but what do you think of producing minimum + maximum (& exclusiveMinimum)? It should be pretty straightforward

The issue is that different OAI client-generators not aware of custom defined formats, they usually rely on min + max bounds (to choose more appropriate type). It's ofc possible to extend rust-specific generators to support these types, but it's still depends a lot on particular library used to generate OAI spec. So usually generators use more common approaches