cancel
Showing results for 
Search instead for 
Did you mean: 

Specifying types which are subset of String

New Contributor

Specifying types which are subset of String

Hi, I would like to specify an API which returns a FooId. A FooId is a 4 character String consisting of ascii alphanum chars.

When I end up generating my API I make it so that the FooId is not a wrapper for a string instead I just return:

{
    fooId: "abcd",
someThingElse: "a",
number: 2
}

Is it possible to specify in the swagger doc (openAPI) that:

  • A FooId should be treated like a string, no wrapping class for example.
  • While preserving the fact that it is a FooId so a client consuming the swagger doc knows which APIs really return strings and which ones are returning a FooId, so no information is lost.

Does the spec support this?

 

full disclosure I asked a similar question on stack overflow which got no responses https://stackoverflow.com/questions/55915610/does-swagger-openapi-support-declaring-types-that-are-s...

 

3 REPLIES 3
Community Hero

Re: Specifying types which are subset of String

It would have been helpful if you post the swagger definition, how you have defined the property.

You may want to take a look at the documentation on regular expression for property and see if that is what you are looking for?
https://swagger.io/docs/specification/data-models/data-types/


Regards,
Rao.
Highlighted
New Contributor

Re: Specifying types which are subset of String

Hi,
So I assume:

    ssn:
      type: string
      pattern: '^\d{3}-\d{2}-\d{4}$'

this is defining something like what I want, where the type is 'ssn' but it is really just a string.

If I was to have a list of `ssn` could it look like:

["AAA-GG-SSSS","AAA-GG-SSSS","AAA-GG-SSSS"]

ie no wrapping class.
So should this mean that something like:

"schema": {
    "type": "array",
    "items": {
        "$ref": "#/definitions/FooId"
    }

and in definitions

"FooId": {
    "type": "string",
    "title": "FooId"
},

would work to define an array of FooId where FooId is really just a string type. When the API responds that looks like:

["abcd", "abcd"]

If this is the case then it looks like at least the standard supports. So now I need to find out why springfox is not making those types.

Was this supported in version 2?

New Contributor

Re: Specifying types which are subset of String

Hi so in the end what I am doing is generating types in the definitions section like so:

FooId : {
     "type": "string",
     "title": "CollectionId"
}

the swagger API UI seems to be happy with that, and I assume that is legit.

New Here?
Join us and watch the welcome video: