Ask a Question

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


Community Hero

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?


So I assume:

      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:


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?

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.

Showing results for 
Search instead for 
Did you mean: