Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Discriminator field is 'null' in subtype #295

Closed
alxgrk opened this issue Aug 14, 2017 · 6 comments
Closed

Discriminator field is 'null' in subtype #295

alxgrk opened this issue Aug 14, 2017 · 6 comments

Comments

@alxgrk
Copy link

alxgrk commented Aug 14, 2017

Hey there,

I tried using the discriminator property in a type like that (shortened example):

Animal:
    type: object
    discriminator: animalType
    properties:
      animalType: string
  
  Cat:
    type: Animal
    properties:
      legs: integer

When I tried generating code from this, the generated field animalType was always null in both, Animal and Cat.
The default discriminatorValue should be the type name (see RAML doku).
This behavior would be helpful here.


In a second step, I tried to add the discriminatorValue manually to Cat like this:

  Cat:
    type: Animal
    discriminatorValue: cat
    properties:
      legs: integer

As you can see, the value is the lower-cased type name "cat". Reasonably, I expected this to be used in the class. However, during generation an exception was thrown:
org.raml.jaxrs.generator.GenerationException: [Error validating JSON. Error: - Invalid type name: Cat

When I changed the discriminatorValue to upper-cased Cat (like the name of the type), it finally worked and the field animalType has the value "Cat" in the class Cat.

However, I expected to be able to use whatever name and not, that the discriminatorValue has to match the class name exactly.

Thanks already for having a look at this issue.
Regards

@jpbelang
Copy link
Contributor

jpbelang commented Aug 22, 2017

I did the first half of the bug. I'll look at the second half. Could you look at the raml-defined example: the Human type doesn't seem to have your problem. And that error message is not from our code (probably the parser...)

Thanks :-)

@alxgrk
Copy link
Author

alxgrk commented Aug 27, 2017

Hey @jpbelang,
sorry for answering that late. The commits fixed the issue, thanks for that. I also tried it with the Human type, which worked as well. However, one problem still remains: the generated implementations for each class now have the string "*Impl" as a type (e.g. "HumanImpl"). I expected this to be the name of the interface class. Is it possible to change this as well?

Not sure about the second part: for me, the solution without discriminatorValue is fine. If you couldn't figure out the problem with this, it would be ok.

Regards

@jpbelang
Copy link
Contributor

Do you mean in the discriminator value ? I get the discriminator
private String humanType = "example.model.Corpse;

which is the interface class.

@alxgrk
Copy link
Author

alxgrk commented Sep 18, 2017

Alright, I finally had time to try it again. It works now as you described, not sure what made it not working as expected on the first go.
I'll close the issue now.

@alxgrk alxgrk closed this as completed Sep 18, 2017
@alxgrk
Copy link
Author

alxgrk commented Sep 18, 2017

When I tested it right now, the String humanType was only containing the class name. However, when I read your comment again, I saw that in you case it was "example.model.Corpse". I don't want the package name here. Could this lead to collisions (e.g. that Corpse could be used equally for two different types)? Or could you maybe simply provide a setter for the discriminator value in order to provide a custom value?
Thanks already

@alxgrk alxgrk reopened this Sep 18, 2017
@alxgrk
Copy link
Author

alxgrk commented Oct 5, 2017

Alright, probably this issue won't find any more recognition, so for me it's fine to declare the discriminatorValue explicitly within the RAML.

@alxgrk alxgrk closed this as completed Oct 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants