🌐
Swagger
swagger.io › docs › specification › v3_0 › data-models › data-models
Data Models (Schemas) | Swagger Docs
API DesignAPI DevelopmentAPI DocumentationAPI TestingAPI Mocking and VirtualizationAPI GovernanceAPI MonitoringOpenAPI & Swagger ... OAS 3 This guide is for OpenAPI 3.0. OpenAPI 3.0 data types are based on an extended subset JSON Schema Specification Wright Draft 00 (aka Draft 5).
🌐
Swagger
swagger.io › docs › specification › v3_0 › adding-examples
Adding Examples | Swagger Docs
If schema refers to some object defined in the components section, then you should make example a child of the media type keyword:
🌐
Goswagger
goswagger.io › go-swagger › reference › models › schemas
Swagger schema generation rules | go-swagger
January 1, 2023 - The discriminator’s possible values are the names of the subtypes (no aliasing is supported in Swagger 2.0). NOTE: the discriminator is a required property with "type": "string". No validation attached to this property, save required, will be honored, as a discriminator property is implicitly an enum with all the subtype names found in the spec. The base type must be a JSON-schema object (it has at least one property, the discriminator). It may define other properties than the discriminator. Like name in this example.
🌐
Swagger-autogen
swagger-autogen.github.io › swagger 2.0 › schemas and definitions
Schemas and Definitions | Swagger Autogen
In the case above, the schema in the .json will be the same in '@schema'. ... const doc = { ... definitions: { myBoolean: true, myNumber: 123, myString: 'my example', myObject: { field: 'my example' }, myArrayOfBooleans: [true], myArrayOfNumbers: [123], myArrayOfStrings: ['my example'], myArrayOfObjects: [ { field: 'my example' } ], myReferencedObjectArray: [{ $ref: '#/definitions/myObject' }] }; }; ... app.get('/responses', (req, res) => { /* #swagger.responses[001] = { description: 'myBoolean', schema: { $ref: '#/definitions/myBoolean' } } */ /* #swagger.responses[002] = { description: 'myNu
🌐
Swagger
swagger.io › docs › specification › v3_0 › basic-structure
Basic Structure | Swagger Docs
- url: http://staging-api.example.com · 11 · description: Optional server description, e.g. Internal staging server for testing · 12 · 13 · paths: 14 · /users: 15 · get: 16 · summary: Returns a list of users. 17 · description: Optional extended description in CommonMark or HTML. 18 · responses: 19 · "200": # status code · 20 · description: A JSON array of user names · 21 · content: 22 · application/json: 23 · schema: 24 ·
🌐
Swagger-autogen
swagger-autogen.github.io › openapi 3.x › schemas and components
Schemas and Components | Swagger Autogen
To reference your schema in the doc object, use the $ref: "#/components/schemas/someSchema", for example: ... app.get('/path', (req, res) => { ... /* #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: ...
🌐
Baeldung
baeldung.com › home › rest › swagger @parameter vs @schema
Swagger @Parameter vs @Schema | Baeldung
January 7, 2026 - Simply put, @Parameter and @Schema ... to Swagger. The @Parameter annotation is for the parameters of an API resource request, whereas @Schema is for the properties of the model. The @Parameter annotation is used for defining parameters in the parameters section of an operation or path. The @Parameter annotation helps to specify the name, description, and example value of the ...
🌐
Swagger
docs.swagger.io › swagger-core › v2.0.0-RC3 › apidocs › io › swagger › v3 › oas › annotations › media › Schema.html
Schema (swagger-annotations 2.0.0-rc3 API)
A description of the schema. ... Provides an optional override for the format. If a consumer is unaware of the meaning of the format, they shall fall back to using the basic type without format. For example, if \"type: integer, format: int128\" were used to designate a very large integer, most ...
🌐
Swagger
swagger.io › specification
OpenAPI Specification - Version 3.1.0 | Swagger
The OpenAPI Specification defines a standard interface to RESTful APIs which allows both humans and computers to understand service capabilities without access to source code, documentation, or network traffic inspection.
Find elsewhere
Top answer
1 of 8
15

Sharing my working approach for the issue, I have done a workaround for the @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class) the Schema is coming as String issue.

I have declared a custom schema called Map in the OpenAPI bean declaration as below

new OpenAPI()
                .components(new Components()
                        .addSchemas("Map", new Schema<Map<String, Object>>().addProperties("< * >", new ObjectSchema())
                        ))
                    .....
                    .....

and used the above schema in the Schema declaration as below

 @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = APPLICATION_JSON_VALUE, 
                 schema = @Schema(ref = "#/components/schemas/Map"))

The above approach can be used in the place of ApiResponse also as below

 @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200",
            content = @Content(mediaType = APPLICATION_JSON_VALUE, 
                 schema = @Schema(ref = "#/components/schemas/Map"))

Note: If we use the above custom schema approach, we don't need to alter or ignore any of the types which SpringDoc is using internally.

2 of 8
10

I have one API endpoint, the request body expects a HashMap. There is not much information on how to fix the "Example value" issue. Prasanth's answer lead me to the right place. I'm posting my solution for completeness but all credit goes to him. (PS: I tried to upvote his answer but I don't have enough "points")

The configurations side:

@Configuration
@OpenAPIDefinition
public class DocsConfiguration {
    @Bean
    public OpenAPI customOpenAPI() {
        Schema newUserSchema = new Schema<Map<String, Object>>()
                .addProperties("name",new StringSchema().example("John123"))
                .addProperties("password",new StringSchema().example("P4SSW0RD"))
                .addProperties("image",new StringSchema().example("https://robohash.org/John123.png"));

        return new OpenAPI()
                //.servers(servers)
                .info(new Info()
                        .title("Your app title")
                        .description("App description")
                        .version("1.0")
                        .license(new License().name("GNU/GPL").url("https://www.gnu.org/licenses/gpl-3.0.html"))
                )
                .components(new Components()
                        .addSchemas("NewUserBody" , newUserSchema)
                );
    }
}

The controller side:

    @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema(ref = "#/components/schemas/NewUserBody")))
    @PostMapping("/v1/users")
    public Response<User> upsertUser(@RequestBody HashMap<String,Object> user) {
         //Your code here
    }
🌐
Swagger
swagger.io › specification › v2
OpenAPI Specification - Version 2.0 | Swagger
Version 2.0 specification defines a set of files required to describe an API. These files can then be used by the Swagger-UI project and Swagger-Codegen. Learn more.
🌐
GitHub
github.com › swagger-api › swagger-core › wiki › OpenAPI-3.0-Schema-Resolution
OpenAPI 3.0 Schema Resolution · swagger-api/swagger-core Wiki · GitHub
October 8, 2024 - Employee: type: object properties: ... homeAddress: $ref: '#/components/schemas/Address' workAddress: $ref: '#/components/schemas/Address' description: Employee · This works fine for the majority of scenarios, however in some situations, given that OpenAPI 3.0 does not support $ref siblings, information can get lost or even be wrong; an occurrence of lost information is already visible in the example above, where description defined in @Schema(description = "Employee parameter") Employee employee is not part of the resolved spec as the description part of the Employee class application takes precedence:
Author   swagger-api
🌐
Swagger
swagger.io › docs › specification › v3_0 › components
Components Section | Swagger Docs
In the example below, duplicate definitions of a User object are replaced with a single component and references to that component. Before: ... components serve as a container for various reusable definitions – schemas (data models), parameters, responses, examples, and others.
🌐
Javadoc.io
javadoc.io › doc › io.swagger.core.v3 › swagger-annotations › latest › io › swagger › v3 › oas › annotations › media › Schema.html
Schema - swagger-annotations 2.2.48 javadoc
Bookmarks · Latest version of io.swagger.core.v3:swagger-annotations · https://javadoc.io/doc/io.swagger.core.v3/swagger-annotations · Current version 2.2.48 · https://javadoc.io/doc/io.swagger.core.v3/swagger-annotations/2.2.48 · package-list path (used for javadoc generation -link option) ...
🌐
Stack Overflow
stackoverflow.com › questions › 56652741 › adding-example-based-on-a-schema
swagger - Adding example based on a schema - Stack Overflow
interfaces: description: A list of the device's interfaces type: array items: type: object required: true schema: $ref: '#components/schemas/Interface' example: #<---- What should I put here? required: true Interface: type: object properties: name: description: Interface's name type: string required: true example: eth0 IP: ... ... What do you mean by "get Swagger to generate an example"?
🌐
Baeldung
baeldung.com › home › rest › setting example and description with swagger
Setting Example and Description with Swagger | Baeldung
May 23, 2025 - Let’s make some changes in the Product class to achieve this: @Schema(name = "Product ID", example = "1", required = true) private Long id; @Schema(name = "Product name", example = "Product 1", required = false) private String name; @Schema(name ...
🌐
Swagger
swagger.io › docs › specification › v3_0 › data-models › data-types
Data Types | Swagger Docs
For example, pattern: pet matches pet, petstore and carpet. The ^…$ token forces an exact match. type: boolean represents two values: true and false. Note that truthy and falsy values such as “true”, "", 0 or null are not considered boolean values. OpenAPI 3.0 does not have an explicit ...
Top answer
1 of 7
90

After longer fight with using Swagger for specifying REST API and reusing it in related test suites, I will share my own experience with it (answering my own question).

Swagger supports only subset of JSON Schema Draft 4

Specification of Swagger 1.2 and 2.0 states, it supports only subset of JSON Schema Draft 4 (s. here). This means, that:

  • one cannot rely, that each valid JSON Schema can be completely supported by Swagger.
  • thinking of XML, Swagger supports only canonical representation of subset of JSON structures provided by JSON Schema Draft 4.

In other words:

  • Swagger (1.2 and 2.0) does not support usage of many JSON structures, which are valid in terms of JSON Schema Draft 4 (the same applies to Draft 3).
  • Swagger does not support general XML data structures, only very restricted structures are allowed.

In practice, you cannot start with designing your data in JSON or XML, with Swagger you have to start and end with Swagger.

Getting JSON Schema is theoretically possible, but not easy

I have spent some time coding a library, which would take Swagger API Specification and create JSON Schema Draft 4. I gave up for couple of reasons:

  • it was not easy at all
  • got disappointed finding, that I can use only subset of what JSON Schema provides. We had some JSON payload already proposed and had to start modifying it just to fit what Swagger specification framework allows.

Apart from having really nice looking UI for showing and testing the API (yes, everybody agrees, it is visually very pleasing), I have found it weird, that a specification framework is not allowing us to use what we want, but adds unexpected restrictions to our design.

If you want full JSON or XML Schema support, use RAML

Researching other API specification frameworks, I have found RAML. As it is built from ground up by supporting any JSON Schema Draft 3/4 or W3C XML Schema 1.0 data structures, the experience was excellent - having structure of my payload designed, I was able authoring API specification very quickly and following validation of real requests and responses against defined schemas was very easy, as the schemas are essentials components of the specification without adding any restrictions on them.

RAML was at version 0.8 that time (version 1.0 was not released yet).

Correcting the question leads to real solution

Good question makes half of the solution. My question was wrong as it missed fulfilling my real expectations. Corrected question would be:

What specification framework and technique to use, to specify REST API using payload defined by arbitrary JSON Schema Draft 4 or W3C XML Schema 1.0.

My answer to such a question would be:

  1. Design your payload in JSON Schema Draft 4 or W3C XML Schema
  2. Describe your REST API by means of RAML (v0.8 at the moment).

There might be other specification frameworks usable, but Swagger (neither v1.2 nor v2.0) is definitely not the case. Apart from providing really a lot of features (code generation, very nice looking documentation of the API and much more), it simply fails in providing solution to the updated question stated above.

2 of 7
17

There is a python tool to do the same by the name openapi2jsonschema. You can simply install it using pip.

The readme for openapi2 shows the simplest way to use it:

openapi2jsonschema https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json

Hope this helps.

🌐
GitHub
gist.github.com › unhurried › 65be039436581b6a6124a5c53b890c43
Swagger 3.0 Example · GitHub
Swagger 3.0 Example · Raw · openapi.yaml · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
Top answer
1 of 2
12

OpenAPI 3.0

Multiple examples are only supported at the media type level and are not supported inside schemas. Schemas and properties can only have a single example, e.g.

partitionProperty:
  type: string
  example: '2016-03-04T03:00:00'

In other words, this won't work:

MainObject:
  type: object
  properties:
    ..
  examples:  # <--- Won't work
    minimal:
      summary: Minimal example
      value:
        foo: bar
    full:
      summary: Example with all properties
      value:
        foo: bar
        baz: xyzzy

If you want multiple examples, you need to use request examples or response examples instead:

requestBody:
  content:
    application/json:
      schema:
        $ref: '#/components/schemas/MainObject'
      examples:
        minimal:
          summary: Minimal example
          value:
            foo: bar
        full:
          summary: Example with all properties
          value:
            foo: bar
            baz: xyzzy

OpenAPI 3.1

OAS 3.1 uses a newer version of JSON Schema which supports multiple examples in schemas and properties. Unlike media type examples which is a map of named Example Objects, schema-level and property-level examples is a plain list of example values.

MyObject:
  type: object
  properties:
    prop1:
      type: string
      # Property-level examples
      examples:
        - foo
        - bar
    prop2:
      type: integer

  # Object-level examples
  examples:
    - prop1: hello
      prop2: 5
    - prop1: world
      prop2: 0
2 of 2
1

If you are stuck on OpenAPI v3.0.x and need (strongly desire) multiple examples, here is a hackish way to provide them within the schemas section (Note single quotes inside and double quotes outside):

common_phone:
  type: string
  maxLength: 14
  minLength: 1
  example: "'(614) 555-1212' or '614.555.1212' or '614-555-1212' or '6145551212'"