如何在Swagger中定义含int、int[]等类型的DataType接口结构
Got it, let's tackle this problem. You're trying to define a Swagger/OpenAPI schema for an interface where the params field is a dictionary with values that can be integers, strings, integer arrays, or string arrays—and you've run into trouble handling both single values and arrays together. Here are two solid solutions depending on your needs:
Solution 1: Simple oneOf for Direct Value Types
If you don't need explicit type identifiers (like your existing _class enum) and just want to validate that values are one of the four allowed types, use oneOf to define the possible value schemas, then attach that to the additionalProperties of your params object.
openapi: 3.0.3 components: schemas: # Define all allowed value types AllowedParamValue: oneOf: - type: integer example: 42 - type: string example: "hello world" - type: array items: type: integer example: [1, 2, 3] - type: array items: type: string example: ["foo", "bar"] # Your main DataType schema DataType: type: object properties: params: type: object # Allow any string keys, with values matching AllowedParamValue additionalProperties: $ref: '#/components/schemas/AllowedParamValue'
This setup directly enforces that every value in params is either an int, string, int array, or string array—no extra wrapper objects needed.
Solution 2: Discriminator for Explicit Type Identification
If you need to keep the _class type identifier (to help clients parse values correctly), expand your discriminator setup to include array types. You'll define a base schema with the discriminator, then create specific schemas for each type (including arrays).
openapi: 3.0.3 components: schemas: # Base schema with discriminator BaseParamValue: type: object discriminator: propertyName: _class mapping: integer: '#/components/schemas/IntegerValue' string: '#/components/schemas/StringValue' integerArray: '#/components/schemas/IntegerArrayValue' stringArray: '#/components/schemas/StringArrayValue' required: - _class # Individual type schemas IntegerValue: allOf: - $ref: '#/components/schemas/BaseParamValue' - type: object properties: value: type: integer example: 42 required: - value StringValue: allOf: - $ref: '#/components/schemas/BaseParamValue' - type: object properties: value: type: string example: "hello world" required: - value IntegerArrayValue: allOf: - $ref: '#/components/schemas/BaseParamValue' - type: object properties: value: type: array items: type: integer example: [1, 2, 3] required: - value StringArrayValue: allOf: - $ref: '#/components/schemas/BaseParamValue' - type: object properties: value: type: array items: type: string example: ["foo", "bar"] required: - value # Your main DataType schema DataType: type: object properties: params: type: object additionalProperties: $ref: '#/components/schemas/BaseParamValue'
With this approach, each value in params is an object with a _class field (telling you if it's an integer, string, etc.) and a value field containing the actual data. This is great if your client code needs to explicitly check the type before processing the value.
内容的提问来源于stack exchange,提问作者jyoti




