Wildcard properties in Swagger?
I have a JSON response which may have any number of nodes, and the names may vary too. For example:
{
thing: "this a thing"
other: "this is another thing"
another: "and yet another thing"
}
Is there a way to indicate it in Swagger that the object may contain any number of properties, each with any name?
type: object
properties:
*:
type: string
repeats: infinitely
See also questions close to this topic
-
Spring boot code generated from swaggerhub is not able to throw 400 on additional properties
I've logged in to swaggerhub and generated a sample inventory API which is part of their template. In
codegen options
selectedspring
as server. ticked onuseBeanValidation
checkbox. Generated the code, tested it. Validations are working properly as expected except only one thing. I don't want to allow any additional property on the POST payload.NB: The openAPI specification doesn't have 'additionalProperties' flag specified. Which defaults it to 'false' in my understanding. I tried to explicitly specify the same too. but no luck. There is no exception thrown while I add an additionalProperty.
The OpenAPI specification
openapi: 3.0.0 servers: # Added by API Auto Mocking Plugin - description: SwaggerHub API Auto Mocking url: https://virtserver.swaggerhub.com/xxxx/apionopenApi3/1.0.0 info: description: This is a simple API version: "1.0.0" title: Simple Inventory API contact: email: you@your-company.com license: name: Apache 2.0 url: 'http://www.apache.org/licenses/LICENSE-2.0.html' tags: - name: admins description: Secured Admin-only calls - name: developers description: Operations available to regular developers paths: /inventory: get: tags: - developers summary: searches inventory operationId: searchInventory description: | By passing in the appropriate options, you can search for available inventory in the system parameters: - in: query name: searchString description: pass an optional search string for looking up inventory required: false schema: type: string - in: query name: skip description: number of records to skip for pagination schema: type: integer format: int32 minimum: 0 - in: query name: limit description: maximum number of records to return schema: type: integer format: int32 minimum: 0 maximum: 50 responses: '200': description: search results matching criteria content: application/json: schema: type: array items: $ref: '#/components/schemas/InventoryItem' '400': description: bad input parameter post: tags: - admins summary: adds an inventory item operationId: addInventory description: Adds an item to the system responses: '201': description: item created '400': description: 'invalid input, object invalid' '409': description: an existing item already exists requestBody: content: application/json: schema: $ref: '#/components/schemas/InventoryItem' description: Inventory item to add components: schemas: InventoryItem: type: object additionalProperties: False required: - id - name - manufacturer - releaseDate properties: id: type: string format: uuid example: d290f1ee-6c54-4b01-90e6-d701748f0851 name: type: string example: Widget Adapter releaseDate: type: string format: date-time example: '2016-08-29T09:12:33.001Z' manufacturer: $ref: '#/components/schemas/Manufacturer' Manufacturer: required: - name properties: name: type: string example: ACME Corporation homePage: type: string format: url example: 'https://www.acme-corp.com' phone: type: string example: 408-867-5309 type: object additionalProperties: False
Got 400 for this payload on post API.
{ "id": "7f125802-7840-4ff0-8ddb-7c78c0948987", "name11111": "Widget Adapter", "releaseDate": "2016-08-29T09:12:33.001Z", "manufacturer": { "name": "ACME Corporation", "homePage": "", "phone": "408-867-5309" } }
Expected 400 but didn't get on this payload on post
{ "id": "7f125802-7840-4ff0-8ddb-7c78c0948983", "name": "Widget Adapter", "releaseDate": "2016-08-29T09:12:33.001Z", "manufacturer": { "name": "ACME Corporation", "homePage": "", "phone": "408-867-5309" }, "newkey" : "newValue" }
Can anybody pls help on this.. I want to completely disallow additional properties and throw a 400 on those requests..
-
Swagger on Spring Boot: documentation is visible only on localhost
I went through many posts on this blog and elsewhere, but I didn't find the solution for my problem, so I hope you'll be able to help me and won't consider this post as a repetition of another one. I developed a Spring Boot application and then added Swagger support, writing two configuration classes.
First one:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SpringFoxConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() // .apis(RequestHandlerSelectors.any()) commentato per non visualizzare API relativi al percorso /error .apis(RequestHandlerSelectors.basePackage("it.ader.GestionaleSAL")) .paths(PathSelectors.any()) .build(); } }
Second one:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
Swagger works perfectly on localhost, at the address http://localhost:8050/swagger-ui/index.html. Anyway, when my application was moved to remote server, I was given the remote URL in the form IP:port. Then I tried from browser IP:port/swagger-ui/index.html, but I got an error page with following content:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Mar 08 17:36:30 CET 2021 There was an unexpected error (type=Not Found, status=404).
Does anyone know how to make Swagger work even on remote server? Is additional configuration required? Someboby states on web that this problem may depend on CORS problems. In such case, how to verify this option and solve?
Thanks for support.
-
gin-swagger can not find schema type: "handler.component"
I am trying to use a struct that I created and I use in my code as the parameter but after running
swag init
I am getting the error:ParseComment error in file /src/handler/handler.go :can not find schema type: "handler.component"
My struct:
package types // Component defines the structure of the json that user will send for a log search // swagger:parameters component type Component struct { // in: query // example: {"envID":"default", "podID":"log-gen-6d776dc797-bnbm9", "follow":false, "tail":5} EnvID string `json:"envID"` // The env-id PodID string `json:"podID"` // The podID Tail int `json:"tail"` // Number of lines for tailing the logs Follow bool `json:"follow"` // If the we want to follow the logs or not Site string `json:"site"` // The cluster/site which hosts the component --> local is pointing to the local cluster }
My handler:
package handler import ( "src/types" ) // FollowLogsSSE is ... // @Summary Return logs // @Accept json // @Produce json // @Param q query component true "{'envID':'default', 'podID':'log-gen-6d776dc797-bnbm9', 'follow':false, 'tail':5}" // @Success 200 {object} string string func FollowLogsSSE(comp types.Component) gin.HandlerFunc { }
I also tried
// @Param q query types.component true "{'envID':'default', 'podID':'log-gen-6d776dc797-bnbm9', 'follow':false, 'tail':5}"
but I got the exact same error.How can I fix this?