README.md in dry-swagger-0.4.0 vs README.md in dry-swagger-0.4.1
- old
+ new
@@ -31,11 +31,11 @@
optional(:optional_nullable_field).maybe(:str?)
optional(:optional_filled_field).filled(:str?)
end
end
- Dry::Swagger::ContractParser.new.call(Contract)
+ Dry::Swagger::ContractParser.new.call(Contract).to_swagger
=> {
"type": "object",
"properties": {
"required_field": {
"type": "string",
@@ -79,11 +79,11 @@
required(:field).value(:str?)
end
end
end
- Dry::Swagger::ContractParser.new.call(Contract)
+ Dry::Swagger::ContractParser.new.call(Contract).to_swagger
=> {
"type": "object",
"properties": {
"array_field": {
"type": "array",
@@ -133,11 +133,11 @@
attribute :required_string_with_enum, Types::String.enum('enum1')
attribute? :optional_string, Types::String
attribute? :optional_nullable_string, Types::String.optional
end
- Dry::Swagger::StructParser.new.call(DTO)
+ Dry::Swagger::StructParser.new.call(DTO).to_swagger
=> {
"type": "object",
"properties": {
"required_string": {
"type": "string",
@@ -168,11 +168,26 @@
"required_nullable_string",
"required_string_with_enum"
]
}
#### With nested fields
- {
+ class NestedDTO < Dry::Struct
+ attribute :required_string, Types::String
+ attribute :required_nullable_string, Types::String.optional
+ attribute :required_string_with_enum, Types::String.enum('enum1')
+ attribute? :optional_string, Types::String
+ attribute? :optional_nullable_string, Types::String.optional
+ end
+
+ class DTO < Dry::Struct
+ attribute :array_of_integer, Types::Array.of(Types::Integer)
+ attribute :array_of_objects, Types::Array.of(NestedDTO)
+ attribute :dto, NestedDTO
+ end
+
+ Dry::Swagger::StructParser.new.call(DTO).to_swagger
+ => {
"type": "object",
"properties": {
"array_of_integer": {
"type": "array",
"items": {
@@ -256,10 +271,48 @@
"array_of_integer",
"array_of_objects",
"dto"
]
}
+## Overriding fields on run time
+You can also modify the fields during runtime by passing a block after the .call() method.
+
+For example:
+
+ Dry::Swagger::StructParser.new.call(DTO) do |it|
+ # types = string/integer/hash/array
+
+ # Remove a field
+ its.keys = it.keys.except(:field_name)
+
+ # Add new field on root level
+ it.keys[:new_field_name] = { type: type, required: true/false, :it.config.nullable_type=>true/false }
+
+ # Add a new field in nested hash/array
+ it.keys[:nested_field][:keys][:new_field_name] = {
+ type: type, required: true/false, :it.config.nullable_type=>true/false
+ }
+
+ # Remove a field in nested hash/array
+ it.keys = it.keys[:nested_field][:keys].except(:field_name)
+
+ # Add an array or hash
+ it.keys[:nested_field] = {
+ type: "array/hash", required: true/false, :it.config.nullable_type=> true/false, keys: {
+ # List all nested fields
+ new_field: { type: :type, required: true/false, :it.config.nullable_type=>true/false }
+ }
+ }
+
+ # Add an Array of primitive types, type field needs to be the element type(string, integer, float),
+ and add an array: true flag
+
+ it.keys[:array_field_name] = {
+ type: type, array: true, required: true/false, :it.config.nullable_type=> true/false
+ }
+
+ end.to_swagger()
## Custom Configuration For Your Project
You can override default configurations by creating a file in config/initializers/dry-swagger.rb and changing the following values.
Dry::Swagger::Config::StructConfiguration.configuration do |config|
config.enable_required_validation = true / false
@@ -268,15 +321,15 @@
config.enable_descriptions = true / false
config.nullable_type = :"x-nullable" / :nullable
end
Dry::Swagger::Config::ContractConfiguration.configuration do |config|
- config.enable_required_validation = true / false
- config.enable_nullable_validation = true / false
- config.enable_enums = true / false
- config.enable_descriptions = true / false
- config.nullable_type = :"x-nullable" / :nullable
- end
+ config.enable_required_validation = true / false
+ config.enable_nullable_validation = true / false
+ config.enable_enums = true / false
+ config.enable_descriptions = true / false
+ config.nullable_type = :"x-nullable" / :nullable
+ end
By default, all these settings are true, and nullable_type is :"x-nullable".
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.