question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defining constant value in response

See original GitHub issue

In order to specify that the response for an API call is always a certain given value, I would like to create this this feature request.

If the client wants to get the details of a non-existing pet of a pet store then the server should say

{
    "status": "ERROR",
    "error_message": "ERROR__PET_NOT_FOUND"
}

The best way I found for describing this is to use enums with only one element, like this:

    schema:
        type: object
        properties:
            result: 
                type: string
                enum: [ERROR]
            error_code: 
                type: string
                enum: [ERROR__PET_NOT_FOUND]

Instead of this, an exact value should be defined with an implicit type detection. So,

    schema:
        type: object
        properties:
            result: 
                value: "ERROR"
            error_code: 
                value: "ERROR__PET_NOT_FOUND"

should mean the same as the previous declaration. The following scalar types should be auto-detected:

  • type = string, if the value is surrounded by " symbols
  • type = integer, if the value contains only digits and sign
  • type = float, if the value contains digits, sign AND decimal point

Moreover, the new value declaration should work one level above as well:

    schema:
        type: object
        properties:
            value: 
                result: "ERROR"
                error_code: "ERROR__PET_NOT_FOUND"

This specification should mean a structure that has the two fields with these two constant values.

One more step would be the following notation:

    schema:
        value: 
            result: "ERROR"
            error_code: "ERROR__PET_NOT_FOUND"

This would determine the type to be an objects and the properties as above.

This notation would be much more dense and easier to both write and understand.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:63
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

110reactions
darrelmillercommented, Aug 5, 2017

If an API had an exact response then it would seem rather redundant to call it. Why even have an API if it returns the same value every time?

57reactions
jminicommented, Feb 20, 2018

Having a constant could be interesting for the Inheritance and Polymorphism case (when a discriminator and a mapping are defined in a oneOf schema). Take this example from the guide:

components:
  responses:
    sampleObjectResponse:
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/Object1'
              - $ref: '#/components/schemas/Object2'
            discriminator:
              propertyName: objectType
              mapping:
                obj1: '#/components/schemas/Object1'
		obj2: '#/components/schemas/Object2'
  …
  schemas:
    Object1:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
      …
    Object2:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
      …

In this case, objectType defined in #/components/schemas/Object1 and in #/components/schemas/Object2 is just present for the discriminator mapping, it would be much nicer to have it defined as constant.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define constant string in Swagger open api 3.0
You can define a constant parameter as a required parameter with only ... In the api all value can be changed if have...
Read more >
Constants and Variables – Programming Fundamentals
Constants are used in two ways. They are: literal constant; defined constant. A literal constant is a value you type into your program...
Read more >
Constants in C Explained – How to Use #define and the const ...
You can define constants in C in a few different ways. ... <VALUE> is a placeholder for the value that <VAR_NAME> takes. #define...
Read more >
Using Variables, Declaring Constants in C++ - InformIT
It is good programming practice to define variables that are not supposed to change their values as const. The usage of the const...
Read more >
const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates block-scoped constants, much like variables declared using the let keyword. The value of a constant can't be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found