Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Add kotlin generator, includes schema generation and validation #431

Merged
merged 78 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
d11490f
Adds kotlin petstore sample
spacether May 2, 2024
8f0c9bc
Changes status to experimental
spacether May 2, 2024
028c529
Updates 2 configuration classes
spacether May 6, 2024
8efbe19
Converts all exceptions to kotlin
spacether May 6, 2024
fd1b0dd
Converts KeywordValidator and supporting classes to Kotlin
spacether May 6, 2024
b930d58
Updates kotlin jsonschema templates and supporting classes
spacether May 6, 2024
c5513c4
Reverts java templates
spacether May 6, 2024
63724fa
Updates AdditionalPropertiesValidator
spacether May 6, 2024
98f6257
Updates UnsetAnyTypeJsonSchema
spacether May 7, 2024
60a42f3
Move improvemnts for kotlin code of json schema and unset anytype schema
spacether May 7, 2024
7ca254d
Updates companion object for unset any type schema
spacether May 7, 2024
3d50a91
More improvements to UnsetAnyTypeJsonSchema
spacether May 7, 2024
c912298
Fixes more types in UnsetAnyTypeJsonSchema1Boxed
spacether May 7, 2024
8e2371f
Updates FrozenList and FrozenMap
spacether May 7, 2024
7a4b722
Updates FrozenList/Map
spacether May 7, 2024
2a472c3
Reverts java template changes
spacether May 7, 2024
585f865
Fixes all of the type validator interfaces
spacether May 7, 2024
fb9dc33
Updates type validator
spacether May 7, 2024
8f3e822
Updates FormatValidator
spacether May 7, 2024
bd8bed4
Updates ItemsValidator
spacether May 8, 2024
05a77b9
Updates RequiredValidator
spacether May 8, 2024
a6a1f1b
Updates ExclusiveMaximumValidator
spacether May 8, 2024
8d2f0b2
Updates ExclusiveMinimumValidator
spacether May 8, 2024
447f1b0
Updates max and min items validators
spacether May 8, 2024
4f1703a
Updates Max and Min length validators
spacether May 8, 2024
11bd256
Updates max and min properties validator
spacether May 8, 2024
6051806
Updates max and min validator
spacether May 8, 2024
7d0d2f2
Fixes some intelij warnings in the jsonschema class
spacether May 8, 2024
d0ac93c
Updates AllOfValidator
spacether May 8, 2024
646db81
Updates anyof and oneof validators
spacether May 8, 2024
f9addbf
Updates not and unique items validators
spacether May 8, 2024
006774b
Updates enum + pattern validators
spacether May 8, 2024
1f7652b
Updates const and contains validator
spacether May 8, 2024
e7b057a
Updates max and min contains validators
spacether May 8, 2024
cd1ac1d
Updates DependentRequiredValidator + PropertyNamesValidator
spacether May 8, 2024
20f5613
Updates DependentSchemasValidator + PatternPropertiesValidator
spacether May 8, 2024
ae18ab2
Updates IfValidator + PrefixItemsValidator
spacether May 8, 2024
643da09
Updates ElseValidator + ThenValidator
spacether May 8, 2024
e21dd34
Updates UnevaluatedItemsValidator + UnevaluatedPropertiesValidator
spacether May 8, 2024
5521ebe
Updates AnyTypeJsonSchema
spacether May 9, 2024
f887577
Updates NotAnyTypeJsonSchema
spacether May 9, 2024
4efe364
Updates BooleanJsonSchema
spacether May 9, 2024
798b77a
Adds NullJsonSchema
spacether May 9, 2024
036ecc9
Updates StringJsonSchema
spacether May 9, 2024
2c451ae
Updates ListJsonSchema
spacether May 9, 2024
ad7cd07
Updates MapJsonSchema
spacether May 9, 2024
3106125
Updates NumberJsonSchema
spacether May 9, 2024
e05b170
Updates DateJsonSchema
spacether May 9, 2024
5eeae6a
Updates DateTimeJsonSchema
spacether May 9, 2024
b1f7250
Updates DecimalJsonSchema
spacether May 9, 2024
bd787ab
Adds DoubleJsonSchema
spacether May 9, 2024
c4abd5e
Updates FloatJsonSchema
spacether May 9, 2024
2ef5e38
Updates GenericBuilder
spacether May 9, 2024
f331bf8
Uses intsAllowedForFloatDoubleFormats in Float/Double schemas
spacether May 9, 2024
3be6a1e
Updates Int32JsonSchema
spacether May 9, 2024
b71e163
Updates Int64JsonSchema
spacether May 9, 2024
9313454
Updates IntJsonSchema
spacether May 9, 2024
4bc351b
Updates SetMaker and UnsetAddPropsSetter
spacether May 9, 2024
bea0ee4
Samples and docs regen
spacether May 17, 2024
458928f
Kotlin 310 sample gen
spacether May 17, 2024
d25216e
Updates dependentSchemas
spacether May 17, 2024
22cd355
Updates dependentRequired
spacether May 17, 2024
1e24856
Updates patternProperties
spacether May 17, 2024
369a9af
Includes patternProperties in calculation of mapValueSchema
spacether May 17, 2024
79bf237
Omits getKnownKeys method when additionalProperties is turned off
spacether May 17, 2024
2ee8809
Fixes value checking of lists of any type in map output getters
spacether May 18, 2024
fbc196b
Fixes output type issues for property getters
spacether May 18, 2024
10e154b
Adds 310 kotlin test to ci
spacether May 19, 2024
651d229
Samples regen
spacether May 19, 2024
731a238
Adjusts gradlew command
spacether May 19, 2024
2fa9be6
Update build file to increase the max jvm memory limit when compiling
spacether May 20, 2024
ec334a6
Removes nodaemon arg on gradle test invocation
spacether May 20, 2024
d56bb22
Adds info logging to 310 gradle test
spacether May 20, 2024
f5ff4d8
Adds info logging
spacether May 20, 2024
b832b95
Increases memory to 512 mB
spacether May 20, 2024
dfc5f47
Increases memory to 1 g
spacether May 20, 2024
e4a788d
Updates template default to 1g
spacether May 20, 2024
9ab973e
Readme updated
spacether May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
24 changes: 20 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ commands: # a reusable command with parameters
# In many cases you can simplify this from what is generated here.
# 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/'
- run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
# This is based on your 1.0 configuration file or project settings
- run:
command: java -version
# Test
# Show jobId
- run:
name: "Setup custom environment variables"
command: echo 'export CIRCLE_JOB_ID="<<parameters.jobId>>"' >> $BASH_ENV
Expand Down Expand Up @@ -137,6 +134,24 @@ jobs:
key: javaClientMavenCache
paths:
- ~/.m2
testKotlinClientSamples:
docker:
- image: cimg/openjdk:17.0.9
working_directory: ~/OpenAPITools/openapi-json-schema-generator
shell: /bin/bash --login
environment:
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
steps:
- restore_cache:
keys:
- kotlinClientGradleCache
- command_build_and_test:
jobId: "testKotlinClientSamples"
- save_cache:
key: kotlinClientGradleCache
paths:
- ~/build
workflows:
version: 2
build:
Expand All @@ -146,3 +161,4 @@ workflows:
- testPython38ClientSamples
- testPython39ClientSamples
- testJava17ClientSamples
- testKotlinClientSamples
7 changes: 7 additions & 0 deletions .circleci/parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ elif [ "$JOB_ID" = "testJava17ClientSamples" ]; then

cat ./.circleci/testJava17ClientSamples.sh | parallel

elif [ "$JOB_ID" = "testKotlinClientSamples" ]; then
echo "Running job $JOB_ID ..."
gradle --version
java -version

cat ./.circleci/testKotlinClientSamples.sh | parallel

else
echo "Running job $JOB_ID"

Expand Down
2 changes: 1 addition & 1 deletion .circleci/testJava17ClientSamples.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(cd samples/client/petstore/java && gradle wrapper && gradle test)
(cd samples/client/petstore/java && gradle wrapper && ./gradlew test --no-daemon)
(cd samples/client/3_0_3_unit_test/java && mvn test)
(cd samples/client/3_1_0_unit_test/java && mvn test)
2 changes: 2 additions & 0 deletions .circleci/testKotlinClientSamples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(cd samples/client/3_0_3_unit_test/kotlin && gradle wrapper && ./gradlew cleanTest test -info)
(cd samples/client/3_1_0_unit_test/kotlin && gradle wrapper && ./gradlew cleanTest test -info)
103 changes: 27 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,89 +15,40 @@ so developers can use all of those features.

Currently, the following languages/frameworks are supported:

- python (Stability: Stable)
- java (Stability: Stable)

## Join Our Community
We use a Discord server as a place to ask questions and help each other. It offers functionality very similar to Slack.
You can join us here: https://discord.gg/mHB8WEQuYQ

## Reasons To Use the Python Generator

- v3.0.0 - [v3.1.0*](#openapi-v310-support) OpenAPI Specification support
- Type hints on
- schema payload inputs in SomeSchema.validate ![validate screen capture](docs/schema_validate.gif)
- Note: to make input dictionaries TypedDicts like the Money.validate example, set additionalProperties to false in the schema in your openapi document
- schema keyword argument inputs in `SomeSchemaDict.__new__` ![validate screen capture](docs/schemadict_new.gif)
- accessing properties in object instances so some_val in some_val = some_inst.someKey will have the correct type hint ![instance properties screen capture](docs/instance_properties.gif)
- accessing array items in array instances so some_val in some_val = array_inst[0] will have the correct type hint
- endpoint inputs + responses
- Run time type checking and validation checking when:
- instantiating models
- sending to endpoints
- receiving from endpoints
- Note: if needed, validation of json schema keywords can be deactivated via a SchemaConfiguration class
- [mypy](samples/client/petstore/python/test_python.sh) runs on sample petstore client and passes
- passing mypy tests means that this generator could be ported into compiled languages like java/kotlin/golang
- [Autogenerated thorough testing of json schema keyword features in models and endpoints](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator/tree/master/samples/client/3_0_3_unit_test/python/test) which come from the [json schema test suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
- [Tests are passing in CI](https://app.circleci.com/pipelines/github/openapi-json-schema-tools/openapi-json-schema-generator?branch=master)
- [Test endpoints are tagged by the relevant keyword like type/format/allOf 39 keywords and counting](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator/tree/master/samples/client/3_1_0_unit_test/python/docs/apis/tags)
- Code re-use built in from the ground up
- components/schemas/headers etc are generated as separate classes and imported when used via $ref
- Openapi spec inline schemas supported at any depth in any location
| Feature | [Python](docs/generators/python.md) | [Java](docs/generators/java.md) | [Kotlin](docs/generators/kotlin.md) |
|----------------------------------------------------------------------------------------------------|-------------------------------------|---------------------------------|-------------------------------------|
| Generator status | stable | stable | experimental |
| Openapi v3.0.0-3.1.0 ingestion | X | X | X |
| Json Schema 2020-12 Support (components/schemas) | X | X | X |
| Component schemas documentation produced | X | X | X |
| Documentation produced for other component types:<br>headers, parameters,requestBodies, ressponses | X | X | |
| Endpoints that send/receive json + docs generated for them | X | X | |

## Reasons To Use the Generators
- Openapi spec support for v3.0.0-3.1.0
- thorough tests run in CI using json schema test suite, see 3_0_0 and 3_1_0 sample clients
- Static analysis:
- mypy run in CI against python petstore sample
- checker framework run w/ NullnessChecker, ensures no null pointer exceptions
- Format support for: int32, int64, float, double, binary, date, datetime, uuid
- Invalid (in python) property names supported like `from`, `1var`, `hi-there` etc in
- Invalid (in language) property names supported like `from`, `1var`, `hi-there` etc in
- schema property names
- endpoint parameter names
- Openapi document inline schemas supported at any depth in any location
- Generated Code: Class + method inputs are typed
- Generated Code: Static type checking done in static languages suing builder inputs and class property access
- Generated Code: run-time type checking done in all generators (a payload can be validated against n schemas)
- Generated Code re-use built in from the ground up
- components/schemas/headers etc are generated as separate classes and imported when used via $ref
- Payload values are not coerced when validated, so a date/date-time value can pass other validations that describe the payload only as type string
- types are generated for enums of type string/integer/boolean using typing.Literal
- String transmission of numbers supported with type: string, format: number, value can be accessed as a Decimal with schemas.as_decimal(inst)
- Multiple content types supported for request and response bodies
- Endpoint response always also includes the urllib3.HTTPResponse
- Endpoint response deserialization can be skipped with the skip_deserialization argument
- Endpoint response always also includes the raw response
- Interfaces kept consistent across generated languages

And many more!
- [Docs for the python generator](docs/generators/python.md)
- [generated client sample code](samples/client/petstore/python)
- [Openapi v3.0.3 general petstore spec, general features](src/test/resources/3_0/python/petstore_customized.yaml)
- [generated v3.1.0 unit test client sample code](samples/client/3_1_0_unit_test/python)
- [Openapi json schema v3.1.0 unit test spec](src/test/resources/3_1/unit_test_spec/3_1_0_unit_test_spec.yaml)
- [generated v3.0.3 unit test client sample code](samples/client/3_0_3_unit_test/python)
- [Openapi json schema v3.0.3 unit test spec](src/test/resources/3_0/unit_test_spec/3_0_3_unit_test_spec.yaml)

## Reasons To Use the Java Generator

- v3.0.0 - [v3.1.0](docs/generators/java.md#schema-feature) OpenAPI Specification support
- Documentation generated in the style of javadocs. Examples: [Money schema](samples/client/petstore/java/docs/components/schemas/Money.md#money1) [Pet.addPet endpoint](samples/client/petstore/java/docs/apis/tags/Pet.md#addpet)
- Sealed classes used to define endpoint responses/response bodies/validated schema payloads/request bodies
- Input types constrained for a Schema in SomeSchema.validate
- validate method can accept arbitrary List/Map/null/int/long/double/float/String json data
- Immutable List output classes generated and returned by validate for List&lt;?&gt; input
- Immutable Map output classes generated and returned by validate for Map&lt;?, ?&gt; input
- Strictly typed list input can be instantiated in client code using generated ListBuilders
- Strictly typed map input can be instantiated in client code using generated MapBuilders
- Sequential map builders are generated ensuring that required properties are set before build is invoked. Looks like:
- `new MapBuilder().requiredA("a").requiredB("b").build()`
- `new MapBuilder().requiredA("a").requiredB("b").optionalProp("c").additionalProperty("someAddProp", "d").build()`
- Run time type checking and validation when
- validating schema payloads
- instantiating List output class (validation run)
- instantiating Map output class (validation run)
- Note: if needed, validation of json schema keywords can be deactivated via a SchemaConfiguration class
- Enums classes are generated and may be input into Schema.validate or the List/MapBuilder add/setter methods
- The [Checker-Framework's](https://github.com/typetools/checker-framework) NullnessChecker and @Nullable annotations are used in the java client
- ensuring that null pointer exceptions will not happen
- Invalid (in java) property names supported like `class`, `1var`, `hi-there` etc in
- component schema names
- schema property names (a fallback setter is written in the MapBuilder)
- Generated interfaces are largely consistent with the python code
- Openapi spec inline schemas supported at any depth in any location
- Format support for: int32, int64, float, double, date, datetime, uuid
- Payload values are not coerced when validated, so a date/date-time value can pass other validations that describe the payload only as type string
- enum types are generated for enums of type string/integer/number/boolean/null
- String transmission of numbers supported with type: string, format: number
- [Autogenerated thorough testing of json schema keyword features in models and endpoints](samples/client/3_0_3_unit_test/java/src/test/java/org/openapijsonschematools/client/components/schemas) which come from the [json schema test suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
- [Tests are passing in CI](https://app.circleci.com/pipelines/github/openapi-json-schema-tools/openapi-json-schema-generator?branch=master)
## Join Our Community
We use a Discord server as a place to ask questions and help each other. It offers functionality very similar to Slack.
You can join us here: https://discord.gg/mHB8WEQuYQ

And many more!
- [Docs for the java generator](docs/generators/java.md)
Expand Down
6 changes: 6 additions & 0 deletions bin/generate_samples_configs/kotlin_3_0_3_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: kotlin
outputDir: samples/client/3_0_3_unit_test/kotlin
inputSpec: src/test/resources/3_0/unit_test_spec/3_0_3_unit_test_spec_nopaths.yaml
intsAllowedForFloatDoubleFormats: true
artifactId: unit-test-api
hideGenerationTimestamp: true
6 changes: 6 additions & 0 deletions bin/generate_samples_configs/kotlin_3_1_0_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: kotlin
outputDir: samples/client/3_1_0_unit_test/kotlin
inputSpec: src/test/resources/3_1/unit_test_spec/3_1_0_unit_test_spec_nopaths.yaml
intsAllowedForFloatDoubleFormats: true
artifactId: unit-test-api
hideGenerationTimestamp: true
Loading