Skip to content

Commit

Permalink
add oneOf discriminator lookup support (#13301)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Aug 28, 2022
1 parent a582c38 commit b55fa26
Show file tree
Hide file tree
Showing 43 changed files with 2,398 additions and 10 deletions.
1 change: 1 addition & 0 deletions bin/configs/r-httr2-wrapper-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ additionalProperties:
errorObjectType: "ModelApiResponse"
operationIdNaming: snake_case
generateWrapper: true
useOneOfDiscriminatorLookup: true
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String errorObjectType;
protected String operationIdNaming;
protected boolean generateWrapper;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup

private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();

Expand Down Expand Up @@ -265,6 +266,12 @@ public void processOpts() {
this.setGenerateWrapper(false);
}

if (additionalProperties.containsKey(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP)) {
setUseOneOfDiscriminatorLookup(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP));
} else {
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup);
}

additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
additionalProperties.put(CodegenConstants.EXCEPTION_ON_FAILURE, returnExceptionOnFailure);
Expand Down Expand Up @@ -623,6 +630,14 @@ public void setGenerateWrapper(final boolean generateWrapper) {
this.generateWrapper = generateWrapper;
}

public void setUseOneOfDiscriminatorLookup(boolean useOneOfDiscriminatorLookup) {
this.useOneOfDiscriminatorLookup = useOneOfDiscriminatorLookup;
}

public boolean getUseOneOfDiscriminatorLookup() {
return this.useOneOfDiscriminatorLookup;
}

public void setOperationIdNaming(final String operationIdNaming) {
if (!("PascalCase".equals(operationIdNaming) || "camelCase".equals(operationIdNaming) ||
"snake_case".equals(operationIdNaming))) {
Expand Down
33 changes: 33 additions & 0 deletions modules/openapi-generator/src/main/resources/r/modelOneOf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@
error_messages <- list()
instance <- NULL
{{#useOneOfDiscriminatorLookup}}
{{#discriminator}}
oneof_lookup_result <- tryCatch({
discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`{{{propertyBaseName}}}`
switch(discriminatorValue,
{{#mappedModels}}
{{{mappingName}}}={
{{{modelName}}}$public_methods$validateJSON(input)
{{{modelName}}}_instance <- {{{modelName}}}$new()
self$actual_instance <- {{{modelName}}}_instance$fromJSON(input)
self$actual_type <- "{{{modelName}}}"
return(self)
}{{^-last}},{{/-last}}{{#-last}})},{{/-last}}
{{/mappedModels}}
{{^mappedModels}}
{{#oneOf}}
{{{.}}}={
{{{.}}}$public_methods$validateJSON(input)
{{{.}}}_instance <- {{{.}}}$new()
self$actual_instance <- {{{.}}}_instance$fromJSON(input)
self$actual_type <- "{{{.}}}"
return(self)
}{{^-last}},{{/-last}}{{#-last}})},{{/-last}}
{{/oneOf}}
{{/mappedModels}}
error = function(err) err
)
if (!is.null(oneof_lookup_result["error"])) {
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. Input: %s", oneof_lookup_result["message"], input))
}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
{{#composedSchemas.oneOf}}
{{^isNull}}
{{{dataType}}}_result <- tryCatch({
Expand Down
49 changes: 41 additions & 8 deletions modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -933,14 +933,6 @@ components:
- $ref: '#/components/schemas/DanishPig'
discriminator:
propertyName: className
OneOfPrimitiveTypeTest:
oneOf:
- type: "integer"
- type: "string"
AnyOfPrimitiveTypeTest:
oneOf:
- type: "integer"
- type: "string"
BasquePig:
type: object
properties:
Expand Down Expand Up @@ -968,3 +960,44 @@ components:
type: integer
nested_pig:
$ref: '#/components/schemas/Pig'
OneOfPrimitiveTypeTest:
oneOf:
- type: "integer"
- type: "string"
AnyOfPrimitiveTypeTest:
oneOf:
- type: "integer"
- type: "string"
mammal:
oneOf:
- $ref: '#/components/schemas/whale'
- $ref: '#/components/schemas/zebra'
discriminator:
propertyName: className
mapping:
whale: '#/components/schemas/whale'
zebra: '#/components/schemas/zebra'
whale:
type: object
properties:
hasBaleen:
type: boolean
hasTeeth:
type: boolean
className:
type: string
required:
- className
zebra:
type: object
properties:
type:
type: string
enum:
- plains
- mountain
- grevys
className:
type: string
required:
- className
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ R/danish_pig.R
R/dog.R
R/dog_all_of.R
R/fake_api.R
R/mammal.R
R/model_api_response.R
R/nested_one_of.R
R/one_of_primitive_type_test.R
Expand All @@ -34,6 +35,8 @@ R/tag.R
R/update_pet_request.R
R/user.R
R/user_api.R
R/whale.R
R/zebra.R
README.md
docs/AllofTagApiResponse.md
docs/Animal.md
Expand All @@ -47,6 +50,7 @@ docs/DanishPig.md
docs/Dog.md
docs/DogAllOf.md
docs/FakeApi.md
docs/Mammal.md
docs/ModelApiResponse.md
docs/NestedOneOf.md
docs/OneOfPrimitiveTypeTest.md
Expand All @@ -60,5 +64,7 @@ docs/Tag.md
docs/UpdatePetRequest.md
docs/User.md
docs/UserApi.md
docs/Whale.md
docs/Zebra.md
git_push.sh
tests/testthat.R
3 changes: 3 additions & 0 deletions samples/client/petstore/R-httr2-wrapper/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(Category)
export(DanishPig)
export(Dog)
export(DogAllOf)
export(Mammal)
export(ModelApiResponse)
export(NestedOneOf)
export(OneOfPrimitiveTypeTest)
Expand All @@ -37,6 +38,8 @@ export(Special)
export(Tag)
export(UpdatePetRequest)
export(User)
export(Whale)
export(Zebra)

# APIs
export(FakeApi)
Expand Down
Loading

0 comments on commit b55fa26

Please sign in to comment.