Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] support for x-const extension #1916

Closed
jmini opened this issue Jan 15, 2019 · 3 comments
Closed

[REQ] support for x-const extension #1916

jmini opened this issue Jan 15, 2019 · 3 comments

Comments

@jmini
Copy link
Member

jmini commented Jan 15, 2019

Description

Sometimes in a Schema, you know that one of the property must be set to a specific value.
For those cases it would be great to be able to define the value as x-const extension in a Schema

Example:

For a user creation call like this one:

POST /user/create

The Request Body looks like this:

{
  "type": "User",
  "id": 132,
  "firstName": "John",
  "lastName": "Doe"
}

For the corresponding UserBody schema, you know that the type attribute must be set to "User".

The OpenAPI Spec would looks like:

components:
  schemas:
    UserBody:
      type: object
      properties:
         type:
          type: string
          x-const: User
        id:
          type: integer
        firstName:
          type: string
        lastName:
          type: string

The OpenAPI Spec is discussing about adding const as allowed keyword. Using it as extension in between allow to have something that works with OpenAPI v3.

Alternative

My understanding is that the current best-practice is to define an enum attribute with a single value in the list.

components:
  schemas:
    UserInput:
      type: object
      properties:
        type:
          type: string
          enum:
            - User
        id:
          type: integer
        firstName:
          type: string
        lastName:
          type: string

This is already supported by OpenAPI-Generator today, but has the drawback that the type needs explicitly to be set:

UserInput u = new UserInput();
u.setType(TypeEnum.USER);
u.setId(132);
u.setFirstName("John");
u.setLastName("Doe");

Swagger UI interprets this pattern correctly and display the value in the UI:

screenshot 2019-01-15 at 14 21 23

Additional context

Related OpenAPI Spec discussions:

@wing328
Copy link
Member

wing328 commented Jan 16, 2019

@jmini the suggestion sounds good to me to start with 👍

@jmini
Copy link
Member Author

jmini commented Jan 17, 2019

I have discovered that in combination with the default attribute, OpenAPI-Generator is producing the expected output.

  type:
    type: string
    enum:
    - User
    default: User

With the rest-assured client:

User u = new User().firstName("John").lastName("Doe");

JSON j = new JSON();
String s = j.serialize(u);

Is producing the expected output:

{"type":"User","firstName":"John","lastName":"Doe"}

I think there is no need to introduce a new extension for this.

This issue can be closed for now, as I have a way to solve my requirement.

@jmini jmini closed this as completed Jan 17, 2019
@jorgerod
Copy link
Contributor

jorgerod commented Sep 20, 2021

Hi @jmini, @wing328 and other colleagues

We need to support this definition of constants.
OpenAPI's Schema Object for OAS 3.1 to use JSON Schema 2019-09, which includes the const keyword

How do you see it? Could this request be reopened?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants